YOUR SAVED CODE-ALONG
Code along: Understand unpacking
Keep the lesson open on your other screen. Your edits stay in this workspace, separate from the practice activity. Sign in to save and reload a private account draft across devices, or share a snapshot when you need help.
CODE-ALONG / THEATER MODE
Understand unpacking
ORIGINAL WALKTHROUGH · 8:43
Watch with Rob.
Pause whenever you want to try a change. You can return to the lesson at any time.
You are signed in, but video access needs attention. Check your account access. Your saved work is kept.
Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.
Video settings & progress
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.
Start the video to enable typing controls.
YOUR TURN
Code along: Understand unpacking
Unpack three items into three names. Compare first, *middle, last with a six-item and a two-item list. Print each result and the unchanged source. Pause the original recording to type and run your examples. This open workspace saves separately from scored practice and has no fixed-answer checks.
Load an example, edit it and run it. Word wrap is enabled.
Your check results
Lesson reference
Examples load into this same code-along only after you confirm replacing your code.
Match the number of names to the number of values
Place the names on the left of = and the collection on the right. Python evaluates the right side, then assigns each item to the corresponding name. The first item goes to first, the next to second and the last to third.
Without a starred target, the counts must match exactly. Too few or too many values raise ValueError. Unpacking does not remove items from the original list.
numbers = [10, 20, 30]
first, second, third = numbers
print(first) # 10
print(second) # 20
print(third) # 30
print(numbers) # unchangedCollect the middle with one starred target
In first, *middle, last = numbers, Python assigns the first item to first, the last item to last and the intervening items to middle. middle is a new list, even if it contains only one item or no items.
This pattern needs at least two items for its two ordinary names. You can use only one starred target in this assignment.
numbers = [1, 2, 3, 4, 5, 6]
first, *middle, last = numbers
print(first) # 1
print(middle) # [2, 3, 4, 5]
print(last) # 6
first, *middle, last = [10, 20]
print(middle) # []Build it one step at a time
Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)4 steps · Saved work · Instant feedback
See the attached lesson’s comic
