Skip to content

Tuples and unpacking

Use a tuple as a fixed record, unpack it into names in an assignment and in a for loop with enumerate and zip, and return two values from a function.

It opens on

a, b = 3, 4 and then a, b = b, a. After the second line, are a and b equal to 4 and 3, equal to 3 and 4, or both the same number?

Why it matters

Pairs and records travel through every Python program, a name with a score, an index with an item, a smallest with a largest, and unpacking is how they are taken apart without index numbers; the one-line swap and the two-value return are everyday idioms.

Good to know first

  • Lists: indexing and mutation: p[0], p[-1] and len(p) read a row of values
  • Aliasing and mutability: a list can be changed through any name that points to it
  • Defining and calling functions: return gives back one value

Step by step

  1. A tuple, and unpacking

    Read a tuple, unpack it into the right number of names, and trace the one-line swap.

    p[0] is "Ada" and p[-1] is 91. But p[1] = 95 raises a TypeError, 'tuple' object does not support item assignment, because a tuple is immutable, like a string. A record that must not change is a tuple.

    A common mix-up

    a, b = b, a leaves both names equal to the old b.

    In fact: The right side is made first from the old values, so the two values trade places.

  2. Unpacking in loops

    Use enumerate to get index and item together, and zip to walk two lists at once, and say what each hands the loop.

    The first loop gets (0, 'Ada'), then (1, 'Bo'), then (2, 'Cy'), so i is the index and n is the item, and it prints 0 Ada, 1 Bo and 2 Cy.

    the pairs from enumerate

    A common mix-up

    Zip and enumerate make new lists of tuples that cost a lot.

    In fact: They hand over one pair at a time as the loop runs.

  3. Returning two values

    Write a function that returns two values as a tuple, call it with unpacking, and say what a tuple's immutability does and does not cover.

    lo and hi both start at the first item. For each v, a smaller one replaces lo and a larger one replaces hi, so the loop is one pass over n items, O(n).

    A common mix-up

    a function can return two values, not one tuple.

    In fact: It returns one tuple, and unpacking at the call takes it apart.

Try it

What do x and y hold after x, y = 10, 20 and then x, y = y, x + y?Answer

20 and 30

Hint: The right side uses the old x and y

What do the lines for k, w in enumerate(["up", "down"]): print(k, w) print?Answer

0 up and then 1 down

Hint: Enumerate counts from 0

What does divmod(23, 4) return, and what are q and rem after q, rem = divmod(23, 4)?Answer

(5, 3), then q is 5 and rem is 3

Hint: 23 = 5 × 4 + 3

Line 4 is a, b = b, a. Python makes the pair of the old values first and unpacks it after. What are a and b afterwards?Answer

a is 4 and b is 3, the two values swapped.

What does zip(names, scores) hand over on its second pass?Answer

n is "Bo" and s is 78, the second item of each list.

Tuples and unpacking

30 minutes every month. No card.