W3docs

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

python— editable, runs on the server

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 result

Why Use Tuple Unpacking?

Concise variable assignment

Without unpacking you need one line per element:

python— editable, runs on the server

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:

python— editable, runs on the server

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)  # 9

Without 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)   # 5

Capture a middle section

first, *middle, last = (10, 20, 30, 40, 50)

print(first)   # 10
print(middle)  # [20, 30, 40]
print(last)    # 50

Only 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 it

Combine _ with a star expression to discard many elements:

first, *_ = (10, 20, 30, 40)

print(first)  # 10
# *_ swallows 20, 30, 40

Nested Tuple Unpacking

If a tuple contains another tuple, you can unpack both levels in one statement by mirroring the nesting with parentheses:

python— editable, runs on the server

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 three

Unpacking 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 cherry

This 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 95

Common 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 assignment

Only one * variable is allowed per unpacking statement.

Quick Reference

PatternExampleWhat it does
Basica, b, c = tAssigns each element to a named variable
Star tailfirst, *rest = tFirst element named; rest captured as a list
Star head*start, last = tLast element named; rest captured as a list
Star middlefirst, *mid, last = tFirst and last named; middle captured as a list
Ignore onex, _, z = tMiddle element discarded by convention
Ignore manyfirst, *_ = tOnly first element kept
Nesteda, (b, c), d = tInner tuple unpacked in the same statement
Loopfor x, y in pairs:Each pair unpacked per iteration

Practice

Practice
What does Python assign to the starred variable when you write `first, *rest = (1, 2, 3, 4, 5)`?
What does Python assign to the starred variable when you write `first, *rest = (1, 2, 3, 4, 5)`?
Was this page helpful?