In Python, we can unpack a tuple with a variable length using star expressions (also known as extended unpacking): a target with * collects all the values that the other targets do not take. Without the star, the number of names must match the number of values, or the assignment raises ValueError.
scores = (90, 85, 77, 64, 50)
first, second, *rest = scores # first = 90, second = 85, rest = [77, 64, 50]
Next, we place the star at different positions, ignore values with _, read the unpacking errors, and use starred unpacking in loops and function results. The examples run on Python 3.14.
1. Unpack a Tuple with Variable Length
Star expressions, introduced in Python 3.0, allow us to assign parts of a tuple to variables while handling the remaining elements as a single entity. The starred target can stand at the beginning, in the middle or at the end.
*head, last = scores # head = [90, 85, 77, 64], last = 50
first, *middle, last = scores # middle = [85, 77, 64]
a, b, *empty = (1, 2) # empty = []
The starred target is always a list, even when the source is a tuple, and it is an empty list when no values are left over. Only one starred target is allowed in an assignment, because Python could not decide how to split the values between two of them.
2. Unpack Tuple and Ignore Unwanted Values
Sometimes we want only some of the values. The underscore (_) is a convention for a variable we do not use, and *_ ignores any number of values.
first, _, third, _, _ = scores # first = 90, third = 77
first, *_, last = scores # first = 90, last = 50
Python still assigns the values to _, so the name is not special to the interpreter. It only tells other developers that the value is not needed.
3. Errors: too many or not enough values to unpack
Without a star, the number of names on the left must equal the number of values on the right. Both directions of a mismatch raise ValueError, and the message says how many values Python expected.
a, b = (1, 2, 3) # ValueError: too many values to unpack (expected 2, got 3)
a, b, c = (1, 2) # ValueError: not enough values to unpack (expected 3, got 2)
a, *b, c = (1,) # ValueError: not enough values to unpack (expected at least 2, got 1)
Two starred targets in one assignment are a SyntaxError (“multiple starred expressions in assignment”), which Python reports before the program runs.
4. Unpacking in Loops and Function Results
Starred unpacking also works in for loops, which helps with rows of different lengths, and with functions that return a tuple.
rows = [("tea", 4, 5, 3), ("milk", 2), ("rice", 9, 8)]
for name, *ratings in rows:
print(name, ratings)
def min_max(values):
return min(values), max(values)
low, high = min_max(scores) # low = 50, high = 90
tea [4, 5, 3]
milk [2]
rice [9, 8]
Unpacking also builds new collections, so (*scores, 40) creates a longer tuple and print(*scores) passes each value as a separate argument. Swapping two variables, a, b = b, a, is plain tuple unpacking as well.
5. Nested Unpacking
When a tuple contains other tuples, the left side can mirror that structure. Parentheses group the inner names.
order = ("Lokesh", ("2026-11-02", "10:30"), 3)
name, (day, hour), qty = order # day = '2026-11-02', hour = '10:30'
6. Unpacking FAQs
The shape of the data on the right side decides what unpacking can do with it.
6.1. Does unpacking work with lists, strings and dictionaries?
Yes, it works with any iterable. A string unpacks into characters, x, y = “ab”, and a dictionary unpacks into its keys, so we use d.items() to get key-value pairs.
6.2. Can we unpack a generator?
Yes. The generator runs until it is exhausted, so first, *rest = (n * n for n in range(4)) gives first = 0 and rest = [1, 4, 9]. An endless generator never finishes.
6.3. Why is the starred variable a list and not a tuple?
PEP 3132 chose a list so that the result is the same type for every source. We convert it with tuple(rest) when a tuple is needed.
7. Conclusion
A starred target collects the values that the plain names leave over, as a list, and _ or *_ discards values we do not need. When unpacking fails, the ValueError message tells us how many values Python expected, and loops and function results use the same rules.
8. References
Happy Learning !!