tuple unpacking

Assigning the items of a tuple or other sequence to several names at once, as in x, y = point.

In more detail

The number of names must match the number of items, or Python raises ValueError. The reverse, writing a, b on the right, packs values into a tuple, so a, b = b, a swaps two variables. It works in a for heading too: for name, colour in pairs:.

Example

for name, age in [("Ana", 30), ("Bo", 25)]:
    print(name, age)

Related terms

Taught in

References

Certification courses