Python Unpacking: Starred Expressions, Swapping, and Destructuring
Imagine you order a package online that contains three items: a book, headphones, and a phone case. When the box arrives, you open it and put each item where it belongs. The book goes on the shelf, headphones go on the desk, and the case goes on your phone.
Python unpacking works the same way. You take a collection of values — a tuple, list, or any iterable — and assign each value to its own variable in one clean line. No need to access items one by one with indices.
In this tutorial, you'll learn basic unpacking, the elegant variable swap trick, starred expressions that capture multiple items, unpacking inside loops, and how to destructure nested data.
How Does Basic Unpacking Work?
When you have a tuple or list with multiple values, you can assign each value to a separate variable in one line. The number of variables on the left must match the number of items on the right.
Without unpacking, you'd need to access each item by index. Unpacking makes the code shorter and much easier to read.
point = (10, 20, 30)
x = point[0]
y = point[1]
z = point[2]
print(x, y, z)point = (10, 20, 30)
x, y, z = point
print(x, y, z)You can also unpack directly in assignments without creating a named tuple first. Python creates the tuple automatically when you separate values with commas.
How to Swap Variables in One Line
In most programming languages, swapping two variables requires a temporary variable. Python's unpacking makes this unnecessary. You can swap values in a single, elegant line.
This works because Python evaluates the right side first, packing the values into a tuple (b, a), and then unpacks them into a, b. The whole thing happens safely in one step.
a = 1
b = 2
temp = a
a = b
b = temp
print(a, b) # 2 1a = 1
b = 2
a, b = b, a
print(a, b) # 2 1You can swap three or more variables the same way. Python handles it all in one step.
What Are Starred Expressions (*rest)?
Sometimes you don't know exactly how many items a collection has, or you only care about a few specific items. The starred expression (*variable) captures any leftover items into a list.
You can place the star in the middle too. This is great when you know the first and last items but want to skip everything between them.
How to Use Unpacking in For Loops
Unpacking shines inside for loops. When you loop over a list of tuples (or lists), you can unpack each item directly into named variables instead of accessing them by index.
This pattern is used constantly with enumerate() and dict.items(). Both return tuples, and unpacking makes the code much cleaner.
How to Unpack Nested Data Structures
Python unpacking can go multiple levels deep. If a value inside your collection is itself a tuple or list, you can unpack it by using nested parentheses on the left side.
This works at any depth. You can even combine nested unpacking with starred expressions for maximum flexibility.
Common Unpacking Mistakes to Avoid
Unpacking is intuitive once you get the hang of it, but there are a few pitfalls that catch beginners.
Practice Exercises
Unpack the tuple point into three variables: x, y, and z. Then print them on one line separated by spaces using print(x, y, z).
The variables first and second have the wrong values. Swap them using Python's unpacking syntax (one line, no temporary variable), then print both.
Use a starred expression to unpack the numbers list into head (the first item) and tail (everything else). Print both on separate lines.
What does this code print? Think about what the star captures and what type it becomes.
a, *b, c = [1, 2, 3, 4, 5, 6]
print(a)
print(b)
print(c)Run the code to verify your prediction.
You have a list of student records. Each record is a tuple of (name, (math_score, english_score)). Use nested unpacking in a for loop to calculate and print each student's average score.
Print each line in the format: Name: average where average is rounded to 1 decimal place using :.1f.