Back
+10 XP
Concept

Star Unpacking: `first, *rest = items`

Slap an asterisk `*` in front of a variable during unpacking to gather all remaining elements into a clean list.

python
first, *middle, last = [1, 2, 3, 4, 5]
print(first)   # 1
print(middle)  # [2, 3, 4]
print(last)    # 5

# 77. Star Unpacking: `first, *rest = items` Slap an asterisk `*` in front of a variable during unpacking to gather all remaining elements into a clean list: ```python first, *middle, last = [1, 2, 3, 4, 5] print(first) # 1 print(middle) # [2, 3, 4] print(last) # 5 ``` This trick lets you peel off the first or last items of arbitrary-length sequences without messing around with manual slices like `items[1:-1]`.

Checkpoint

In `leader, *followers = ['Alice', 'Bob', 'Charlie']`, what does `followers` contain?

Practice

Given `scores = [100, 85, 70, 60]`, unpack the top score into `winner` and the rest into `others`. Print `winner`.

Terminal
01
Tap code blocks below to assemble
Available Blocks