Unpacking Tuples in Python: A Comprehensive Guide
Learn Python tuple unpacking: basic syntax, star expressions, nested tuples, loop unpacking, and common gotchas with clear, runnable examples.
Tuple unpacking (also called iterable unpacking) lets you assign the elements of a tuple — or any iterable — to individual variables in a single statement. This page covers everything from the basic syntax to advanced patterns like star expressions, nested unpacking, and loop-based unpacking.
What is Tuple Unpacking?
When you unpack a tuple, Python maps each element to a corresponding variable on the left-hand side of =. The number of variables must exactly match the number of elements (unless you use a star expression, covered below).
The parentheses around the tuple are optional — 1, 2, 3 is also a tuple. Both of these lines are equivalent:
a, b, c = (1, 2, 3)
a, b, c = 1, 2, 3 # same resultWhy Use Tuple Unpacking?
Concise variable assignment
Without unpacking you need one line per element:
Swapping variables
Unpacking makes swapping two values a one-liner. Python evaluates the right-hand side completely before assigning, so no temporary variable is needed:
Reading function return values
Functions that return multiple values actually return a tuple. Unpacking gives each value a meaningful name immediately:
def min_max(numbers):
return min(numbers), max(numbers)
lo, hi = min_max([3, 1, 4, 1, 5, 9, 2, 6])
print(lo) # 1
print(hi) # 9Without unpacking you would write result[0] and result[1], which is less readable.
Extended Unpacking with the Star Operator
When you do not know (or do not care about) the number of middle elements, prefix one variable with * to capture everything that does not match a named slot. The starred variable always receives a list, even if it captures zero elements.
Capture a tail
first, *rest = (1, 2, 3, 4, 5)
print(first) # 1
print(rest) # [2, 3, 4, 5]Capture a head
*start, last = (1, 2, 3, 4, 5)
print(start) # [1, 2, 3, 4]
print(last) # 5Capture a middle section
first, *middle, last = (10, 20, 30, 40, 50)
print(first) # 10
print(middle) # [20, 30, 40]
print(last) # 50Only one starred variable is allowed per unpacking statement. Using two or more raises a SyntaxError.
Ignoring Elements with _
Python convention uses _ as a throwaway variable name. It is a valid identifier — Python just assigns to it — but by convention it signals "I do not need this value."
x, _, z = (10, 99, 30)
print(x) # 10
print(z) # 30
# _ holds 99 but we do not use itCombine _ with a star expression to discard many elements:
first, *_ = (10, 20, 30, 40)
print(first) # 10
# *_ swallows 20, 30, 40Nested Tuple Unpacking
If a tuple contains another tuple, you can unpack both levels in one statement by mirroring the nesting with parentheses:
This works to any depth, but deep nesting hurts readability — consider unpacking in stages instead.
Unpacking in for Loops
You can unpack each element of an iterable directly in the for statement. This is particularly useful with lists of tuples:
pairs = [(1, 'one'), (2, 'two'), (3, 'three')]
for num, word in pairs:
print(num, word)
# 1 one
# 2 two
# 3 threeUnpacking with enumerate()
enumerate() returns (index, value) pairs, which you can unpack directly:
fruits = ('apple', 'banana', 'cherry')
for i, fruit in enumerate(fruits):
print(i, fruit)
# 0 apple
# 1 banana
# 2 cherryThis avoids manual index tracking and is the idiomatic Python pattern for looping over a sequence when you need both the position and the value.
Unpacking with zip()
zip() pairs elements from two iterables, and you can unpack each pair in the loop:
names = ('Alice', 'Bob', 'Carol')
scores = (92, 87, 95)
for name, score in zip(names, scores):
print(name, score)
# Alice 92
# Bob 87
# Carol 95Common Errors
Too many or too few values
Python raises ValueError if the number of variables does not match the number of elements (and no star expression is present):
a, b = (1, 2, 3)
# ValueError: too many values to unpack (expected 2)a, b, c = (1, 2)
# ValueError: not enough values to unpack (expected 3, got 2)Fix the mismatch or use a star expression to absorb extra elements.
Two starred variables
first, *middle, *last = (1, 2, 3, 4)
# SyntaxError: multiple starred expressions in assignmentOnly one * variable is allowed per unpacking statement.
Quick Reference
| Pattern | Example | What it does |
|---|---|---|
| Basic | a, b, c = t | Assigns each element to a named variable |
| Star tail | first, *rest = t | First element named; rest captured as a list |
| Star head | *start, last = t | Last element named; rest captured as a list |
| Star middle | first, *mid, last = t | First and last named; middle captured as a list |
| Ignore one | x, _, z = t | Middle element discarded by convention |
| Ignore many | first, *_ = t | Only first element kept |
| Nested | a, (b, c), d = t | Inner tuple unpacked in the same statement |
| Loop | for x, y in pairs: | Each pair unpacked per iteration |
Related Chapters
- Python Tuples — creating, indexing, and slicing tuples
- Access Tuples — reading individual elements by index
- Loop Tuples — iterating over tuple elements
- Join Tuples — combining multiple tuples
- Tuple Methods —
count()andindex() - Update Tuples — working around immutability