thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

02.01 · Unpack and edit lists / Watch · 14 min

Give each list item its own name

Unpacking assigns items to names in order. A starred name collects the remaining items in a list.

Activity 1 of 4 in Unpack and edit lists

All courses

Python 3: Lists / 02.01

Understand unpacking

Not started

Match values to names, then collect the remaining values with a starred target.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

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.

Rob assigns three items to three names, then groups middle items between two endpoints.
Rob’s comic strip · Unpack and edit lists

ORIGINAL WALKTHROUGH · 8:43

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.

Open video & code editor (opens in a new tab)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

main.py
main.pypython · example
# Code along with Rob: 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.
# Predict each result, then press play.

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.

Match the number of names to the number of values
lists.pypython · example
numbers = [10, 20, 30]
first, second, third = numbers
print(first)   # 10
print(second)  # 20
print(third)   # 30
print(numbers)  # unchanged
Open workspace to paste this example (opens in a new tab)

Collect 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.

Collect the middle with one starred target
lists.pypython · example
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)  # []
Open workspace to paste this example (opens in a new tab)

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

YOUR TURN

Unpack colors and numbers

Unpack three colors, then first/middle/last from six numbers. Preserve both inputs and print the three colors, first number, middle list and last number.

Expected output
red
green
blue
10
[20, 30, 40, 50]
60

Keep the starting inputs. Print these lines in this order, without extra output.

  • Store the requested colors
  • Store the requested numbers
  • Store the requested first color
  • Store the requested second color
  • Store the requested third color
  • Store the requested first number
  • Store the requested middle numbers
  • Store the requested last number
  • Output 1 matches the requested checkpoint
  • Output 2 matches the requested checkpoint
  • Output 3 matches the requested checkpoint
  • Output 4 matches the requested checkpoint
  • Output 5 matches the requested checkpoint
  • Output 6 matches the requested checkpoint

Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.

Ready to checkRun the checks to see your results here.

Need a nudge?

Match colors to names

Put all three names on the left and colors on the right of one assignment.

Collect everything between endpoints

Use a star before middle_numbers in the left-hand names. The remaining values are collected as a new list.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

Ready to try it?

Practice in your browser and see your results.

Progress details

Have a question?

Ask the Code Zone community.

Ask a question in The Code Zone (opens in a new tab)

Continue learning

ONE STEP FORWARD

Count the required names, then decide whether any remaining items should be collected together.