Python SnakeBytes: Unpacking

codemouse92

Jason C. McDonald

Posted on November 5, 2019

Python SnakeBytes: Unpacking

Collections, and most other iterables, can be unpacked into individual names, by assigning the collection to a comma-separated list of names.

composers = ['Dvorak', 'Tian', 'Richter', 'Beethoven', 'Holst', 'Elgar']

first, second, third, fourth, fifth, sixth = composers
print(first)  # prints 'Dvorak'
print(second)  # prints 'Tian'
print(third)  # prints 'Richter'
print(fourth)  # prints 'Beethoven'
print(fifth)  # prints 'Holst'
print(sixth)  # prints 'Elgar'
Enter fullscreen mode Exit fullscreen mode

There must be enough names on the left side of the assignment operator to hold all the values. Otherwise, a ValueError is raised.

A single starred expression, a name preceded with an asterisk, may be used in the list of names to capture multiple values.

first, second, third, *rest = composers
print(first)  # prints 'Dvorak'
print(second)  # prints 'Tian'
print(third)  # prints 'Richter'
print(rest)  # prints ['Beethoven', 'Holst', 'Elgar']
Enter fullscreen mode Exit fullscreen mode

All of the non-starred names will be assigned to first, and the remainder placed in the starred name.

The name _ is conventionally used for values that are being ignored.

first, _, _, _, _, last = composers
print(first)  # prints 'Dvorak'
print(last)  # prints 'Elgar'
Enter fullscreen mode Exit fullscreen mode

Like any name, _ can also be starred:

first, *_, last = composers
print(first)  # prints 'Dvorak'
print(last)  # prints 'Elgar'
Enter fullscreen mode Exit fullscreen mode

Unpacking is also employed in for loops, generator expressions, and argument lists. Anywhere an iterable is being assigned, unpacking can be used.

CAUTION: Do not attempt to unpack an infinite iterator using starred expressions. It can cause the interpreter (or even the whole system) to lock up.

💖 💪 🙅 🚩
codemouse92
Jason C. McDonald

Posted on November 5, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related