How to Unpack List and Tuples in Python

Unpacking is a Python feature that allows you to assign elements from an iterable (like a list or tuple) directly to multiple variables in a single, clean line of code. This eliminates the need for manual indexing and lets you use meaningful variable names immediately.
For example, suppose you have a list of numbers::
1
2
# Unpacking a list
numbers = [1, 2, 3]
Normally, you might access the values one by one using index notation like this:
1
2
3
x = numbers[0]
y = numbers[1]
z = numbers[2]
While this works, it’s unnecessarily verbose. Using unpacking, you can assign all three in one go:
1
2
3
x, y, z = numbers
print(x, y, z) # Output: 1 2 3
As you can see, Python automatically matches the number of variables on the left-hand side with the number of elements in the list. The unpacking assignment assigns the first element to the first variable, the second element to the second variable, the third element to the third variable, in the order as defined.
This makes it easy to extract multiple values in one line. For the list [1, 2, 3], numbers
contains three elements → [1, 2, 3]
, So Python assigns:
x
= 1y
= 2z
= 3
You can also unpack tuples the same way:
1
2
3
4
5
6
person = ("Alice", 25, "Blue")
name, age, favorite_color = person
print(name) # Alice
print(age) # 25
print(favorite_color) # Blue
Here, each value in the tuple is unpacked into its own variable, making the code both clean and descriptive.
A Practical Example
Think of unpacking like opening a lunchbox 🍱 that contains a sandwich, fruit, and a drink.
Instead of reaching in multiple times, you can take them all out neatly in one motion:
1
sandwich, fruit, drink = lunchbox
Each item from the lunchbox goes directly into its own named variable — organized, efficient, and with no leftovers.
Using *
for Extended Unpacking
You can also use the *
(star) operator, to unpack selected or specific elements from a list. The technique is perfect when you need certain parts of a sequence, especially useful when the total length is unknown.
For example, to get the first item, last item, and all the middle items from a list:
1
2
3
4
5
6
7
numbers = [1, 2, 3, 4, 5]
first, *middle, last = numbers
print(first) # 1
print(middle) # [2, 3, 4]
print(last) # 5
In this example, first
captures the first element (1
), last
captures the last element (5
), and middle
becomes a list containing all remaining elements in between [2, 3, 4]
.
This approach, known as extended unpacking, works with any iterable — including tuples, strings, and other sequences.
Ignoring Values with _
Sometimes you don’t need all the values when unpacking. To ignore unwanted ones, you can define the underscore (_
) as a throwaway variable to upack the elements you want to ignore.
For example, suppose you only need the first two values from a list:
1
2
3
4
5
6
person = ("Bob", 30, "Canada", "Engineer")
name, age, *_ = person
print(name) # Bob
print(age) # 30
In this example, the remaining values (i.e., second and third values - “Canada”, “Engineer”) are captured by *_
and discarded.
Unpacking Strings
Strings are also iterable, so you can unpack their characters too:
1
2
3
letters = "ABC"
a, b, c = letters
print(a, b, c) # Output: A B C
Each variable receives a single character in order.
Unpack First Few, Capture the Rest
Assuming you only care about the first few items in a sequence, you can capture the rest using the star operator:
1
2
3
4
5
6
colors = ["red", "blue", "green", "yellow", "purple"]
first_color, *other_colors = colors
print(first_color) # red
print(other_colors) # ['blue', 'green', 'yellow', 'purple']
Placing an asterisk before *other_colors
instructs Python to collect any remaining elements into a list. This pattern is especially useful for extracting only the values you need while discarding the rest.
Conclusion
Unpacking is a powerful Python feature that simplifies variable assignment and improves readability.
It works with lists, tuples, strings, and any iterable, letting you:
- Assign multiple values in one line
- Ignore values you don’t need
- Capture the rest using
*
By mastering unpacking, you can write Python code that’s both clean and elegant — just like a pro. ✨
To continue building your skills, your next step is to learn about How to Assign Multiple Variables in Python in detail.