Stop 05 · Built-in Data StructuresTopics · 8/9

Unpacking

Part of the Python Roadmap

In short

Unpacking lets you pull values out of a tuple, list or function result straight into named variables. It turns three clumsy index lines into one clear line, and it's everywhere in professional Python.

Unpacking assigns the items of a sequence to several names at once, as in a, b = b, a. A starred name like first, *rest collects whatever is left over, and it works in for loops over pairs too.

The jaadu unstacks a shiny three-tier steel tiffin, placing each container onto its own plate along a table.

Think of it like this

Opening a stacked tiffin box

A steel tiffin has three tiers: roti, sabzi, dal. You open it and set each tier on its own plate in one go. If you only care about the first tier, the rest can go together on one big plate. That's a, b, c = tiffin, and first, *rest = tiffin.

Watch it happen

first, *middle, last = marks

55
0
72
1
81
2
64
3
90
4

1/4We have five marks in a list.

The big ideas

What you need to know

Counts must match

x, y = (1, 2, 3) fails with ValueError. The number of names must equal the number of items, unless you use a star.

Star collects the rest

head, *tail = items puts everything after the first item into a list. Only one starred name is allowed.

Swap in one line

a, b = b, a swaps two values without a temporary variable.

Unpack in loops

for name, marks in results: unpacks each pair as you loop. Use _ for values you don't need.

See it in code

Walk through the code

python
1point = (12.97, 77.59)2lat, lon = point3print(lat, lon)4 5a, b = 5, 96a, b = b, a7print(a, b)                      # 9 58 9top, *others = ["Isha", "Arjun", "Neel", "Tara"]10print(top)                       # Isha11print(others)                    # ['Arjun', 'Neel', 'Tara']12 13results = [("Isha", 94), ("Arjun", 88), ("Neel", 79)]14for name, score in results:15    print(f"{name}: {score}")16 17def min_max(nums):18    return min(nums), max(nums)19 20low, high = min_max([40, 75, 62])21print(low, high)                 # 40 7522 23year, _, day = "2026-11-24".split("-")24print(year, day)                 # 2026 24

1/6Two items, two names. Each gets its own value.

Unpacking coordinates, swapping, star-collecting the rest, looping over pairs and catching two return values.

Save yourself the pain

Common mistakes

Oops

Writing name, age = ['Riya', 21, 'Pune'] and getting 'too many values to unpack'.

Do this instead

Match the count, or use name, age, *_ = ... to ignore the extras.

Oops

Using two starred names like *a, *b = items.

Do this instead

Only one starred target is allowed per unpacking, because Python can't know how to split the rest.

Your mission

Unpack a cricket scorecard

~15 min

Quick check

After first, *rest = 'abc', what is rest?

Go deeper

  1. 1Swap two variables in one line with a, b = b, a.
  2. 2Use first, *middle, last = marks to split a list.
  3. 3Loop over dict.items() and unpack each key and value.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge