Python Unpacking · Exercises
Beginner

Python Unpacking Exercises

4 exercises that build on the Python Unpacking lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: Unpacking assigns the items of any iterable to several names in one statement, and a * lets one name collect the leftovers or spread items into a list, dictionary or function call. By the end you can unpack lists, strings and nested data, collect extras with a starred name, swap values, unpack in for loops, and use * and ** in calls and merges. You will practise: Unpack a record; Predict the unpacking; Fix top_and_rest; Summarize a log.

0 / 4 completed

Python Unpacking practice exercises

1. Unpack a record

Write code Easy

Write three statements, each using unpacking:

  1. Unpack the list record into the names name, city and age.
  2. Unpack date.split("-") into year, month and day.
  3. low and high were typed the wrong way round. Swap them with one statement, so that low is 3 and high is 9.

Finally print name and year.

Your code
record = ["Asha", "Pune", 28]
date = "2025-03-14"
low, high = 9, 3

# your code here

2. Predict the unpacking

Predict the output Medium

Read the code and type exactly what it will print, line by line. Remember that a starred name always collects its items into a list.

Read this code
a, *b = "hello"
print(a, b)

x, (y, z), *w = 1, (2, 3), 4, 5
print(x, y, z, w)

first, *_, last = range(5)
print(first, last)

p, q = 1, 2
p, q = q, p + q
print(p, q)

3. Fix top_and_rest

Fix the bug Medium

top_and_rest(scores) should return a tuple (top, rest): the highest score, and a list of all the other scores from highest to lowest. It only works for lists of exactly two scores and crashes with a ValueError for anything else. Fix it.

Your code
def top_and_rest(scores):
    ordered = sorted(scores, reverse=True)
    top, rest = ordered
    return top, rest

top, rest = top_and_rest([72, 95, 88, 64])
print(top, rest)

4. Summarize a log

Write code Hard

Each log line looks like DATE LEVEL message words..., separated by single spaces. The message can have many words, or be missing entirely.

Write summarize_log(lines) that returns a tuple (first_date, last_date, errors):

  • first_date and last_date are the dates of the first and the last line (the lines are in time order). They are the same when there is only one line.
  • errors is a list with the message (words joined by single spaces) of every line whose level is "ERROR", in order. A missing message becomes "".
  • For an empty list return (None, None, []).

Use unpacking with a star to split each line.

Your code
def summarize_log(lines):
    # each line: "DATE LEVEL message words..."
    pass

log = [
    "2025-03-01 INFO server started",
    "2025-03-01 ERROR disk full on node 3",
    "2025-03-02 WARN slow response",
    "2025-03-03 ERROR timeout",
]
print(summarize_log(log))

Next lessonPython Sorting

More Python Unpacking examples · All Python exercises