BeginnerCoder

Lesson 15 — Unpacking tricks

Star unpacking

Normal unpacking needs exactly one variable per item. Put a star in front of one name and it collects all the leftover items into a list. The star can sit at the start, the middle or the end.

*rest, last = [10, 20, 30]
print(last)  # 30
print(rest)  # [10, 20]

Why it matters: it splits sequences cleanly — the first line of a file, the last part of a path — without fiddly indexing.

Want it another way?
Task

Unpack the first item into head and everything else into rest, then print both.

Run your code — when the output matches the task, you will see a green check. Then mark the lesson complete.

← Back
Next →
Test your knowledge

A quick 3-question quiz on this lesson — instant feedback.

Enjoying this lesson? Share it.