Tuple unpacking in Python lets you assign multiple values from a tuple to multiple variables in a single, clean line of code. It improves readability and is very common when working with structured data.
Instead of accessing each element by index, you can unpack them directly into variables: fruits = ("apple", "banana") → a, b = fruits
The number of variables on the left must match the number of items in the tuple (unless you use the asterisk *).
Using * in unpacking collects remaining values into a list. This is useful when you care about some values and want to group the rest.
The * does not have to be at the end. You can place it in the middle to capture “middle” values.
You can unpack tuples inside tuples in one statement, which is powerful when working with structured or nested data.
General form of tuple unpacking:
# Basic tuple unpacking syntax
(var1, var2, var3, ...) = tuple_name
With asterisk for collecting remaining values:
# Unpack first, middle (as list), and last values
(first, *middle, last) = tuple_name
With nested tuples:
# Unpack outer and inner tuple values
(name, (lang1, lang2), age) = data_tuple
Unpack three fruit names stored in a tuple into three separate variables.
fruits = ("apple", "banana", "cherry")
(a, b, c) = fruits
print(a) # apple
print(b) # banana
print(c) # cherry
Use * to capture extra values into a list called rest.
fruits = ("apple", "banana", "cherry", "orange", "kiwi")
(a, b, *rest) = fruits
print(a) # apple
print(b) # banana
print(rest) # ['cherry', 'orange', 'kiwi']
Here, the first and last elements get their own variables, and the middle values go into a list.
fruits = ("apple", "banana", "cherry", "orange", "kiwi")
(a, *middle, b) = fruits
print(a) # apple
print(middle) # ['banana', 'cherry', 'orange']
print(b) # kiwi
Unpack a tuple that contains another tuple inside it.
data = ("John", ("Python", "Java"), 25)
(name, (lang1, lang2), age) = data
print(name) # John
print(lang1) # Python
print(lang2) # Java
print(age) # 25
From the examples above, the outputs will be:
apple
banana
cherry
apple
banana
['cherry', 'orange', 'kiwi']
apple
['banana', 'cherry', 'orange']
kiwi
John
Python
Java
25
rest becomes a list containing all remaining elements.middle collects all items between the first and last.* to collect extras.* will always be a list, not a tuple.*others.("Alice", ("C", "C++", "Python"), 22) and unpack it into meaningful variables.* at different positions: at the start, middle, and end of the unpacking._, second, *rest = my_tuple.(x, y) = point.