typestar

Unpacking, further in Python

Starred targets, nested shapes, and unpacking into calls.

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

(name, (tours, steps)) = ("python", (11, 143))
print(name, tours, steps)


def describe(lang, tours=0, steps=0):
    return f"{lang}: {tours} tours, {steps} steps"


args = ["rust"]
kwargs = {"tours": 12, "steps": 181}
print(describe(*args, **kwargs))
print([*middle, *[9, 10]], {**kwargs, "steps": 200})

How it works

  1. A starred name absorbs whatever is left over.
  2. Nested patterns mirror the structure exactly.
  3. * and ** in a call spread a sequence or a mapping.

Keywords and builtins used here

The run, in numbers

Lines
15
Characters to type
367
Tokens
145
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 87 seconds.

Type this snippet

Step 2 of 3 in Expressions, step 66 of 72 in Language basics.

← Previous Next →