Use first, second = values to assign a list’s items to variables from left to right. The number of targets must match the number of items unless you use one starred target, such as first, *rest = values, to collect a variable-length remainder. Python also uses * to expand iterables into a new list and to unzip rows with zip(*rows).
Assign list items to variables
In an assignment, Python unpacks the iterable on the right into the targets on the left, in order. The right-hand value does not have to be a list, but it must provide the required number of items. The commas define the targets; parentheses or square brackets can group them.
As an Amazon Associate I earn from qualifying purchases.
values = [10, 20]
first, second = values
# first == 10
# second == 20
With two targets, the iterable must contain exactly two items. If it contains fewer or more, Python raises a ValueError. For fixed-size data, make the target count reflect the expected shape of the input.
Collect a variable-length remainder with *
Put one starred target on the left to collect the items not assigned to the other targets. The starred target always receives a list, which can be empty.
#1 Best Overall
values = [10, 20, 30, 40]
first, *middle, last = values
# first == 10
# middle == [20, 30]
# last == 40
The starred target can appear at the start, middle, or end of the target list. Every non-starred target still needs an item; only the starred target can absorb a remainder of zero or more items.
head, *tail = [10]
# head == 10
# tail == []
Expand values into a new list
In a list display, prefix an iterable expression with * to insert its elements into the new list. This builds a new list and does not mutate the source lists.
Rank #2
left = [1, 2]
right = [3, 4]
combined = [*left, *right]
# combined == [1, 2, 3, 4]
Each expanded expression must be iterable. Trying to expand a non-iterable value, such as an integer, raises an exception.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallUnzip rows of pairs with zip(*pairs)
If a sequence contains pairs, zip(*pairs) passes each pair to zip as a separate input. The result groups the first items together and the second items together:
pairs = [(1, "a"), (2, "b")]
numbers, letters = zip(*pairs)
# numbers == (1, 2)
# letters == ('a', 'b')
The columns returned by zip are tuples. Ordinary zip stops as soon as its shortest input is exhausted, so unequal input lengths can silently leave later items unused. If the inputs are expected to be equal in length, use strict=True to raise a ValueError when they are not. If you want to pad shorter inputs instead, use itertools.zip_longest().
Quick Recap
Best Value
numbers, letters = zip(*pairs)
# For parallel iterables that must have equal lengths:
for number, letter in zip(numbers_source, letters_source, strict=True):
...
Choose the right unpacking form
| What you want | Use | Key behavior |
|---|---|---|
| Assign a fixed number of items to names | a, b = values |
Requires exactly two items. |
| Assign some items and capture the remainder | head, *rest = values |
rest is a list and may be empty; all non-starred targets still need items. |
| Insert iterable contents into a new list | [*values] |
Requires an iterable and creates a list display. |
| Turn paired rows into columns | zip(*pairs) |
Produces tuples; ordinary zip truncates to its shortest input. |
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




