Editor

Predict the Unpacking Output

Intermediate · Predict the output · from Python Unpacking: Starred Expressions, Swapping, and Destructuring

From the lesson: 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.

Read the lesson

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.

# What does this print?
a, *b, c = [1, 2, 3, 4, 5, 6]
print(a)
print(b)
print(c)

More Unpacking exercises

All Python exercises · 50 practice problems · Data Structures quiz