Skip to main content

Python Unpacking: Starred Expressions, Swapping, and Destructuring

Intermediate20 min5 exercises70 XP
0/5 exercises

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.

Basic unpacking from tuples and lists
Loading editor...

Without unpacking, you'd need to access each item by index. Unpacking makes the code shorter and much easier to read.

Without unpacking (verbose)
point = (10, 20, 30)
x = point[0]
y = point[1]
z = point[2]
print(x, y, z)
With unpacking (clean)
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.

Unpacking without parentheses
Loading editor...

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.

Swapping two variables
Loading editor...

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.

Other languages (need temp variable)
a = 1
b = 2
temp = a
a = b
b = temp
print(a, b)  # 2 1
Python (one line)
a = 1
b = 2
a, b = b, a
print(a, b)  # 2 1

You can swap three or more variables the same way. Python handles it all in one step.

Rotating three variables
Loading editor...

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.

Starred expressions capture leftover items
Loading editor...

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.

Star in the middle
Loading editor...
Star with zero items and strings
Loading editor...

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.

Unpacking tuples in a for loop
Loading editor...

This pattern is used constantly with enumerate() and dict.items(). Both return tuples, and unpacking makes the code much cleaner.

Unpacking with enumerate() and dict.items()
Loading editor...

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.

Nested unpacking with parentheses
Loading editor...

This works at any depth. You can even combine nested unpacking with starred expressions for maximum flexibility.

Nested unpacking in loops
Loading editor...

Common Unpacking Mistakes to Avoid

Unpacking is intuitive once you get the hang of it, but there are a few pitfalls that catch beginners.

Common unpacking mistakes
Loading editor...

Practice Exercises

Unpack a Coordinate
Write Code

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).

Loading editor...
Swap Two Variables
Write Code

The variables first and second have the wrong values. Swap them using Python's unpacking syntax (one line, no temporary variable), then print both.

Loading editor...
Head and Tail with Star
Write Code

Use a starred expression to unpack the numbers list into head (the first item) and tail (everything else). Print both on separate lines.

Loading editor...
Predict the Unpacking Output
Predict Output

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.

Loading editor...
Unpack Student Records
Write Code

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.

Loading editor...