Concepts / Multiple Return Values and Tuples

Multiple Return Values and Tuples

Next, we access each key-value pair of the dictionary using the items method of the dictionary which returns a list of tuples where each tuple contains a pair of items - the key followed by the value. We retrieve this pair and assign it to the variables name and address correspondingly for each pair using the for..in loop and then print these values in the for-block.

  • Programming

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.

Group: Place the name first and the address second in one tuple.

Receive: Treat the tuple as one grouped result that contains two values.

Separate: 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.

items()first elementsecond elementDictionaryname → addressTuplename, addressnamekeyaddressvalue
What does each dictionary item look like after calling items(), and how are the key and value grouped together in a tuple?

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.

first valuesecond valueprintprintKey-value tuplename, addressnamefirst elementPrinted valuesname and addressaddresssecond element
How do the two values in each tuple move into the variables 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.

selectunpackcontinueunpackrepeatItemskey-value tuplesTuple 1name, addressname and addressassigned for current tupleNext iterationrepeat assignmentTuple 2name, address
What happens during each loop iteration as the next tuple is selected and its values are assigned?

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.

returnunpackitems()unpackFunctionreturns tupleTupletwo valuesTwo variablesunpacked valuesDictionaryitems()Key-value tuplekey, valueLoop variablesunpacked values
How are multiple values grouped together and then handled as one tuple?

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

  • Reversing the receiving variables

    The tuple places the key first and the value second, so reversing the variables gives each value the wrong role.

    Fix: Match the first variable with the key and the second variable with the value.

  • Treating each dictionary item as an unstructured value

    The items result is described as tuples, and each tuple contains a key-value pair.

    Fix: Recognize the pair as one tuple with a first element and a second element, then unpack it into two variables.

  • Forgetting that assignment repeats

    The loop successively processes each key-value tuple.

    Fix: Trace one tuple at a time and update the two loop variables for each iteration.

Practice the Trace

EASY

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.
  1. A tuple groups multiple values so they can be handled together.
  2. A function can return two different values by returning a tuple.
  3. The items method provides dictionary key-value pairs as tuples in the source material.
  4. The key is the first tuple element and the value is the second.
  5. Tuple 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.