Destructuring assignment

suggest change

In assignments, you can split an Iterable into values using the “unpacking” syntax:

Destructuring as values

a, b = (1, 2)
print(a)
# Prints: 1
print(b)
# Prints: 2

If you try to unpack more than the length of the iterable, you’ll get an error:

a, b, c = [1]
# Raises: ValueError: not enough values to unpack (expected 3, got 1)

Destructuring as a list

You can unpack a list of unknown length using the following syntax:

head, *tail = [1, 2, 3, 4, 5]

Here, we extract the first value as a scalar, and the other values as a list:

print(head)
# Prints: 1
print(tail)
# Prints: [2, 3, 4, 5]

Which is equivalent to:

l = [1, 2, 3, 4, 5]
head = l[0]
tail = l[1:]

It also works with multiple elements or elements form the end of the list:

a, b, *other, z = [1, 2, 3, 4, 5]
print(a, b, z, other)
# Prints: 1 2 5 [3, 4]

Ignoring values in destructuring assignments

If you’re only interested in a given value, you can use \_ to indicate you aren’t interested. Note: this will still set \_, just most people don’t use it as a variable.

a, _ = [1, 2]
print(a)
# Prints: 1
a, _, c = (1, 2, 3)
print(a)
# Prints: 1
print(c)
# Prints: 3

Ignoring lists in destructuring assignments

Finally, you can ignore many values using the *_ syntax in the assignment:

a, *_ = [1, 2, 3, 4, 5]
print(a)
# Prints: 1

which is not really interesting, as you could using indexing on the list instead. Where it gets nice is to keep first and last values in one assignment:

a, *_, b = [1, 2, 3, 4, 5]
print(a, b)
# Prints: 1 5

or extract several values at once:

a, _, b, _, c, *_ = [1, 2, 3, 4, 5, 6]
print(a, b, c)
# Prints: 1 3 5

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


List destructuring:
* Destructuring assignment

Table Of Contents
2 Filter
3 List
7 Loops
22 Reduce
27 Classes
31 Set
42 Tuple
45 Enum
62 Sockets
89 urllib
92 Idioms
104 Stack
105 Profiling
109 Logging
111 os module
116 List destructuring
118 Mixins
120 ArcPy
126 Arrays
132 2to3 tool
135 Unicode
138 Neo4j
140 Curses
141 Templates
145 heapq
146 tkinter
154 Audio
155 pyglet
157 ijson
160 Flask
161 Groupby
163 pygame
165 hashlib
166 Gzip
167 ctypes
185 pyaudio
186 shelve