Concepts / Multiple Return Values and Tuples

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.

  • 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.

Key point

A tuple groups multiple values together. This makes it possible for a function to return two different values as one grouped result.

Worked example

Grouping a Name and an Address

A result needs to carry a person's name and address together.

  1. 1
    Group

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

  2. 2
    Receive

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

  3. 3
    Separate

    Assign the first value to a name variable and the second value to an address variable.

Result

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.

Dictionaryname → addressTuplename, addressnamekeyaddressvalueitems()first elementsecond element
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.

Key-value tuplename, addressnamefirst elementPrinted valuesname and addressaddresssecond elementfirst valuesecond valueprintprint
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.

Itemskey-value tuplesTuple 1name, addressname and addressassigned for current tupleNext iterationrepeat assignmentTuple 2name, addressselectunpackcontinueunpackrepeat
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.

Functionreturns tupleTupletwo valuesTwo variablesunpacked valuesDictionaryitems()Key-value tuplekey, valueLoop variablesunpacked valuesreturnunpackitems()unpack
How are multiple values grouped together and then handled as one tuple?
Best practice

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 like

    Assigning the first tuple element to address and the second to name.

    Why it fails

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

    Do this instead

    Match 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 like

    Ignoring that an item contains two related elements.

    Why it fails

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

    Do this instead

    Recognize 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 like

    Reasoning only about the first pair in the dictionary.

    Why it fails

    The loop successively processes each key-value tuple.

    Do this instead

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

Related concepts

More concepts starting with M