Skip to content
Foxipa

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.

Learning

Python Iterable Unpacking

Learn how Python assigns items from an iterable to multiple targets, including starred targets, nested patterns, and clean value swapping.

Levels: Intermediate ~16 min read

Intermediate

Python assignment can do more than bind one name. When the target on the left contains multiple names, Python can unpack an iterable and assign its items to those targets. This is assignment-side unpacking; it is different from the * and ** forms used to unpack values into a function call.

Match targets to items

With a regular target list, the iterable must provide exactly as many items as there are targets. Assignment proceeds from left to right.

python basic-unpacking.py
1
2
3
4
5
point = (3, 4)
x, y = point

print(x)  # 3
print(y)  # 4

The first item goes to x; the second goes to y.

The right-hand side can be any iterable, not just a tuple or list. Python obtains its items in iteration order and assigns each item to the corresponding target.

python iterable-source.py
1
2
3
4
letters = "cat"
a, b, c = letters

print(a, b, c)  # c a t

Strings are iterable, so their characters can be unpacked too.

The number of items must fit

Without a starred target, Python requires an exact match. Too few or too many items raises ValueError. This is useful because an unexpected data shape fails immediately instead of silently dropping or inventing values.

python exact-count.py
1
2
3
values = [10, 20, 30]
first, second, third = values
print(first + second + third)  # 60

An exact three-item iterable fits three targets.

Collect the rest with a starred target

Prefix one target with * to collect all remaining items. There can be at most one starred target in an assignment target list, and the starred target always receives a list. The list may be empty.

python starred-target.py
1
2
3
4
5
6
numbers = [10, 20, 30, 40, 50]
first, *middle, last = numbers

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

The starred target absorbs everything between the required first and last values.

python starred-empty.py
1
2
3
4
5
6
7
8
9
values = [7, 8]
first, *rest = values
print(first)  # 7
print(rest)   # [8]

value = [7]
only, *rest = value
print(only)  # 7
print(rest)  # []

A starred target can receive an empty list when no items remain.

Unpack nested structure

Targets can themselves contain another target list. Python recursively unpacks each nested iterable, so a pattern can describe structured data without manual indexing.

python nested.py
1
2
3
records = [("Ada", 10), ("Grace", 20)]
for (name, score) in records:
    print(name, score)

Each pair is unpacked again into name and score.

Unpacking can swap values

A multiple-target assignment can also swap existing bindings without a temporary variable. Python evaluates the right-hand side before assigning the targets.

python swap.py
1
2
3
4
5
6
left = "red"
right = "blue"

left, right = right, left
print(left)   # blue
print(right)  # red

The right-hand values are produced first, then assigned to the targets.

Keep assignment unpacking separate from call unpacking

In x, y = point, the star—when present—is a target that collects items from the iterable being assigned. In func(*point), the star is part of a function call and expands an iterable into positional arguments. The two forms use the same symbol for different language operations.

What to remember

A regular target list needs an exact number of items. A single starred target can absorb the remaining items into a list. Nested target lists mirror nested data, and multiple-target assignment can swap values cleanly. These rules belong to assignment syntax, not to the separate function-call unpacking lesson.

Was this page helpful?

Mark this learning resource complete to track your learning progress.