Multiple Return Values and Tuples
A tuple groups multiple values together. This makes it possible for a function to return two different values as one grouped result.
One Result, Several Values
A function does not have to give you only one useful piece of information. It can return two different values by grouping them in a tuple. The tuple lets those values travel together as one result, while the receiving code can later handle the values separately.
A tuple groups multiple values together. This makes it possible for a function to return two different values as one grouped result.
Grouping a Name and an Address
A result needs to carry a person's name and address together.
- 1Group
Place the name first and the address second in one tuple.
- 2Receive
Treat the tuple as one grouped result that contains two values.
- 3Separate
Assign the first value to a name variable and the second value to an address variable.
The name and address remain grouped in one tuple while also becoming available as separate values.
Dictionary Items as Tuples
A dictionary has an items method for accessing its key-value pairs. The source material describes the result as a list of tuples. Each tuple contains one pair: the key comes first and the value comes second. The tuple therefore preserves the relationship between a particular key and its corresponding value.
The important structure is the order inside each tuple. The key is first, followed by the value. That order is what allows the two elements to be assigned correspondingly to two variables.
Unpacking During Iteration
A for-in loop can use two iteration variables when it processes the tuples produced by items. For each tuple, the first element is assigned to the first variable and the second element is assigned to the second variable. In the source explanation, these variables are named name and address.
This assignment happens again for every tuple in the collection. The loop does not treat the pair as one indivisible value: it takes the two positions in the tuple and places them into the two matching variables.
Returning Tuples from Functions
Multiple return values and dictionary iteration use the same central idea: several values can be grouped into a tuple and handled together. A function can return a tuple containing two values. A receiving statement can then unpack that tuple into two variables, just as the loop unpacks each dictionary item into name and address.
When unpacking a tuple, keep the variable order aligned with the tuple order. If the tuple stores the key first and the value second, the first receiving variable should represent the key and the second should represent the value.
Mistakes with Tuple Assignment
Common mistake Reversing the receiving variables
What it looks likeAssigning the first tuple element to address and the second to name.
Why it failsThe tuple places the key first and the value second, so reversing the variables gives each value the wrong role.
Do this insteadMatch the first variable with the key and the second variable with the value.
Common mistake Treating each dictionary item as an unstructured value
What it looks likeIgnoring that an item contains two related elements.
Why it failsThe items result is described as tuples, and each tuple contains a key-value pair.
Do this insteadRecognize the pair as one tuple with a first element and a second element, then unpack it into two variables.
Common mistake Forgetting that assignment repeats
What it looks likeReasoning only about the first pair in the dictionary.
Why it failsThe loop successively processes each key-value tuple.
Do this insteadTrace one tuple at a time and update the two loop variables for each iteration.
Practice the Trace
A dictionary's items method produces a tuple containing a key followed by its value. In a loop, the tuple is assigned to the variables name and address. Explain which tuple position goes into each variable, and describe what happens when the loop selects the next dictionary item.
Hints
- Start with the tuple order: key first, value second.
- Match the first position with name and the second position with address.
- The same assignment process occurs again for the next tuple.
- 1A tuple groups multiple values so they can be handled together.
- 2A function can return two different values by returning a tuple.
- 3The items method provides dictionary key-value pairs as tuples in the source material.
- 4The key is the first tuple element and the value is the second.
- 5Tuple unpacking assigns those positions to separate variables during each loop iteration.
Key Takeaways
- Tuples group multiple values into one structured result.
- Functions can use tuples to return two different values together.
- Dictionary items are represented in the source material as key-value tuples.
- Tuple unpacking maps the first and second tuple elements to separate variables.
- A for-in loop repeats this unpacking for every key-value pair.