liveBook · Manning

Tuple unpacking

As explained in Python How-To

Unpacking is the technique of retrieving the individual items of an iterable and assigning them to corresponding variables.

Need every field from a short tuple? Assign them in one line instead of indexing each position separately: `task_id, task_title, task_urgency = task`. Each item goes to the corresponding variable. This is tuple unpacking: the reverse of packing several items into a tuple. The same technique works with lists and other iterables. section 4.4 section 4.4.1

task = (1001, "Laundry", 5)
task_id, task_title, task_urgency = task

print(task_id, task_title, task_urgency)
# output: 1001 Laundry 5

user_data = ("python_user", 35, "male")
username, age, gender = user_data
print(username, age, gender)
# output: python_user 35 male

Compare the tuple fields with the variables on the left: their positions match, and one assignment replaces several indexed assignments. section 4.4.1

For one-to-one unpacking, provide a variable for each item. Parentheses are optional on either side of a tuple assignment: `x, y = 90, 20` works as well as `x, y = (90, 20)`. Keep multiple assignments together when the variables are closely related; separate unrelated assignments for readability. section 4.4.1

Use only the fields you need

Suppose `task` is `(1001, "Laundry", "Wash clothes", "completed")`. Write `task_id, _, _, task_status = task` when only the ID and status matter. Underscores conventionally mark unwanted values; they are still valid variable names, so repeating `_` overwrites its earlier value. To skip the middle as a group, use `task_id, *_, task_status = task`. section 4.4.3

Unpack values produced elsewhere

A function that appears to return two values returns one tuple. Assign its components directly: `m_mean, m_std = generate_stats(measures)`. Likewise, `for num_pos, num in enumerate(prime_numbers, start=1):` assigns the position and number on each iteration. section 6.2.3 section 1.4.1

The assignment pattern can also follow nested data. For `[1, (2, 3), 4]`, write `a, (b, c), d = [1, (2, 3), 4]` to extract all four numbers in one line. Choose a pattern that makes the data’s shape and the values you intend to use clear. section 4.4.5 section 4.4.4

How Python How-To covers it

How deep it goes
Works through one-to-one assignments, unused positions, starred expressions, and nested data without examining implementation internals.
Examples
Uses task records, statistics returned from a function, numbered prime numbers, and a nested list.
What it assumes
Builds on indexing and slicing, then extends unpacking beyond tuples to other iterables.
What you can do after
Apply it via Python assignment to extract tuple fields and returned values while marking unused positions.
How it connects
Connects tuple packing and function return values with sequence indexing, iterables, and enumerate.

Also explained in

Compare how 3 books explain tuple unpacking →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.