The unpacking assignment assigns elements of a tuple/list to variables at once.
In [1]:
x, y, z = (1, 2, 3)
print(x)
print(y)
print(z)
Use _
to capture values to be ignored.
In [3]:
x, y, _ = (1, 2, 3)
print(x)
print(y)
A variable prefixed with *
captures all left values.
In [6]:
x, *y, z = (1, 2, 3, 4)
print(x)
print(y)
print(z)
*
and _
can be combined togerther to ignore multiple values.
In [2]:
x, *_, z = (1, 2, 3, 4)
print(x)
print(z)