Multiple Assignment
Concepts covered: Unpacking
One assignment statement can set several names. The left side lists the names separated by commas, the right side lists values the same way, and Python pairs them up by position. Python evaluates the entire right side first, and only then binds the names, from left to right.
low, high = 10, 20
print(low, high)
low, high = high, low
print(low, high)
width = height = 8
print(width * height)Output:
10 20 20 10 64
Line 3 is the swap. Python evaluates high, low to the values 20 and 10 while both names still hold their old values, then assigns 20 to low and 10 to high. Without this form, a swap needs a third variable to park one value while the other moves across.
temp = low
low = high
high = tempValues that come from expressions
quotient, remainder = 17 // 5, 17 % 5
print(quotient, remainder)
print(quotient * 5 + remainder)Output:
3 2 17
Each value on the right can be a full expression. Division and remainder belong together, so computing them in one statement keeps the 2 results next to each other and makes the relationship visible: 3 groups of 5 plus 2 left over rebuilds 17. The last line checks that arithmetic.
Chained assignment: one value, several names
width = height = 8 binds the value 8 to height and to width. Both names point at the same object, which costs nothing when the value is a number, because a number cannot change. It matters as soon as the value can.
a = b = []
a += [1]
print(a, b)
c, d = [], []
c += [1]
print(c, d)Output:
[1] [1] [1] []
a = b = [] creates 1 list and 2 names for it, so extending through a shows up through b, exactly as in the previous section. c, d = [], [] evaluates 2 separate [] expressions and creates 2 lists. Chained assignment suits numbers and strings, and lists only when you mean to share one; for anything changeable that should stay independent, give each name its own value.
Mismatched counts on each side
| Statement | What Python raises |
|---|---|
| `x, y = 1, 2, 3` | `ValueError: too many values to unpack (expected 2)` |
| `x, y, z = 1, 2` | `ValueError: not enough values to unpack (expected 3, got 2)` |
Python checks the count before binding anything, so a mismatch leaves every name on the left as it was. The message states how many names were waiting and, for the shortfall case, how many values arrived. Read the number after expected as the count of names on the left side.
One statement versus several
a, b = 3, 4
a, b = b, a + b
print(a, b)
a, b = 3, 4
a = b
b = a + b
print(a, b)Output:
4 7 4 8
The paired form computes a + b with the old a, which is 3, giving 7. The separate statements update a first, so a + b becomes 4 + 4. Whenever a new value depends on an old value that is also being replaced, the single statement is the one that reads every old value.
What does this program print?
x, y = 5, 2
x, y = y, x * y
print(x, y)- 2 4
- 5 10
- 2 10
- A ValueError traceback
A swap works on text as well as on numbers, since the right side only reads the old values. The order of the names on the right is the order in which the names on the left receive them.
About this lesson
This section is part of the Python Expressions: Intermediate lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.