Imagine you have a suitcase (a list) with three items inside. To use them, you could rummage through the bag every time using index numbers: item1 = suitcase[0], item2 = suitcase[1].

Or, you can simply unpack the suitcase and put every item exactly where it belongs in one clean motion. Unpacking makes your code more readable and prevents the “off-by-one” errors that happen when you use indexes.

You list the variables on the left and the iterable (list or tuple) on the right. Python automatically maps them in order.

coordinates = [10.5, 20.2]

# Instead of x = coordinates[0]...
x, y = coordinates

print(x) # 10.5
print(y) # 20.2

In most languages, swapping two variables requires a “temporary” third variable. In Python, you can swap them in a single line using unpacking:

a = 1
b = 2

a, b = b, a 

print(a) # 2
print(b) # 1

The number of variables on the left must match the number of values on the right. If they don’t, Python will throw a ValueError.

# ERROR: ValueError: too many values to unpack
x, y = [1, 2, 3]

Sometimes you only care about a specific part of the data. To tell other developers (and Python) that a value is intentionally being ignored, we use the underscore (_).

This is common when a function returns data you don’t need, like a database ID.

user_data = ("Alice", 28, "ID-1234")

# We only care about the name and age
name, age, _ = user_data

print(f"{name} is {age}")

What if you have a long list but you only need the first item? You can use the asterisk (*) to “catch” all the remaining items and group them into a new list.

numbers = [1, 2, 3, 4, 5]
first, *rest = numbers

print(first) # 1
print(rest)  # [2, 3, 4, 5]

The * is flexible. You can put it at the beginning, middle, or end. Python is smart enough to figure out which values belong to which variable.

# Grabbing the first and the last, and bunching the middle
first, *middle, last = [10, 20, 30, 40, 50]

print(first)  # 10
print(middle) # [20, 30, 40]
print(last)   # 50

You can combine * and _ to grab the parts you want and explicitly tell Python to throw away the rest of a huge dataset.

# We only want the first two items of a long report
title, date, *_ = ["Annual_Report", "2026-01-25", "data1", "data2", "data3..."]

print(title) # Annual_Report