Legends ofPythos
Claim your name
Pythonic Python

Unpacking like a Python programmer

Lesson 2 of 12

Watch the lesson1:10 · with Sigrid
Mental model: Think of unpacking as opening a gift box where each item is already labeled for you. Instead of digging through layers, Python hands you each part directly into the variables you named.

Unpacking pairs

orders = [(101, 'Laptop'), (204, 'Mouse')]
for order_id, product in orders:
    print(f"Order {order_id}: got a {product}")

# Order 101: got a Laptop
# Order 204: got a Mouse
Each tuple splits into two variables automatically.
Python matches the structure of each item to your variable names. This is cleaner than writing for o in orders: oid = o[0]; prod = o[1]. It reads like a sentence, not an index lookup.

Starred unpacking

temperatures = [22, 20, 24]
first_day, *later_days = temperatures
print(f"First: {first_day}°")
print(f"Rest: {later_days}")

# First: 22°
# Rest: [20, 24]
* collects the remaining items into a list.
You can place *rest in other positions too. For example, first, *middle, last = [10, 20, 30] gives you the first and last elements separately, with everything else collected into a list.

Unpacking dicts

def format_user(name, age, city):
    return f"{name} is {age} from {city}"

profile = {'name': 'Ada', 'age': 36, 'city': 'London'}
print(format_user(**profile))
# Ada is 36 from London
** spreads dict keys as keyword arguments.
The ** operator takes each key-value pair and passes it as a named argument. This keeps your function signatures clean while letting callers pass structured data without hardcoding every field name.

Ignoring values

scores = [85, 92]
for _, score in enumerate(scores):
    print(f"Score: {score}")

# Score: 85
# Score: 92
_ says out loud that the index is not needed here.
When you don't need a value, name it _. It signals intent: "I'm here because I have to be." This keeps your code readable and avoids lint warnings about unused variables. You're telling future readers exactly which parts of the data matter.

Your turn

0 of 3 solved

Exercise 1

+30 XP
Write a function sum_pairs(pairs) that takes a list of two-element tuples and returns the sum of all first elements plus twice each second element.

For example, if pairs = [(1, 2), (3, 4)], return 1 + 2*2 + 3 + 2*4 = 16.
Write your Python here.

Run your code to check it against the tests.

Exercise 2

+30 XP
Write a function split_list(nums) that takes a non-empty list and returns a tuple (first, rest) where first is the first element and rest is a list of all remaining elements.

Use starred unpacking to do this in one assignment.
Write your Python here.

Run your code to check it against the tests.

Exercise 3

+30 XP
Write a function format_user(user) that takes a dict with keys 'name', 'age', and 'city' and returns the string "Name (Age) - City".

Use ** unpacking to pass the dict into a helper function called build(name, age, city) which you must also define.
Write your Python here.

Run your code to check it against the tests.