Sub topic 7
Multiple assignment and swapping values
This chapter assumes you know basic Python variables and how a tuple is packed and unpacked. It covers assigning several names in one statement, swapping two values without a temporary variable, and the order in which Python evaluates these statements.
Assigning several names at once
A single assignment statement can bind more than one name. The right side is a tuple, and the left side is a list of targets.
x, y = 3, 4
name, age, city = "Ada", 36, "London"
print(x, y)
print(name, age, city)The statement x, y = 3, 4 does the same job as x = 3 followed by y = 4. The difference shows up when the right side uses the names being assigned.
Swapping without a temporary variable
Many languages need a third variable to swap two values. Python does not.
a = "left"
b = "right"
a, b = b, a
print(a, b)This prints right left. The one-line swap is safe because of how Python orders the work.
- Python evaluates the whole right side first. Here that packs the current values of
bandainto a tuple. - Python then assigns the items of that tuple to the targets, from left to right.
By the time a is rebound, the old value of a is already stored in the tuple.
Compare with sequential assignment
Writing two separate statements gives a different result, because the first assignment destroys information the second one needs.
a, b = 1, 2
a = b
b = a
print(a, b)
a, b = 1, 2
a, b = b, a
print(a, b)The first pair of statements prints 2 2, because b = a copies the new value of a. The tuple version prints 2 1.
Updates that depend on each other
Because the right side uses the old values, multiple assignment suits updates where each new value depends on several old ones. The Fibonacci step is the classic case.
a, b = 0, 1
for _ in range(8):
print(a, end=" ")
a, b = b, a + b
print()This prints 0 1 1 2 3 5 8 13. Both b and a + b are computed from the old a and b before either name changes.
Swapping items inside a list
The targets can be any assignable expression, including list elements. This is how in-place algorithms such as sorting swap neighbors.
values = [5, 2, 9]
values[0], values[2] = values[2], values[0]
print(values)This prints [9, 2, 5].
Targets are assigned left to right
The right side is evaluated completely first. The targets are then assigned one at a time, from left to right. If a later target depends on an earlier one, the earlier assignment has already happened.
i = 0
a = [10, 20, 30]
i, a[i] = 1, 99
print(i, a)This prints 1 [10, 99, 30]. Python assigned i = 1 first, so a[i] then meant a[1]. The code is legal, but readers often expect a[0] to change. Avoid statements where a target's index or key is also a target.
Choosing when to use it
Multiple assignment reads well when the names belong together, such as coordinates, a running pair of values, or a swap. If you assign more than three or four names in one line, split the statement. A long line of targets makes it hard to match each name to its value.
If the counts on the two sides differ, Python raises ValueError. The later chapter on unpacking errors covers that case.
Exercise
What do the lists show after this code runs, and why? a = [1, 2, 3] then a[0], a[1] = a[1], a[0] + 10
Show answer
a becomes [2, 11, 3]. Python first evaluates the right side with the old values, giving the tuple (2, 11). It then assigns 2 to a[0] and 11 to a[1]. The a[0] + 10 used the old a[0], which was 1.
Exercise
Write one statement that rotates three names so that a gets b's value, b gets c's value, and c gets a's old value.
Show answer
a, b, c = b, c, a. The right side is evaluated into a tuple of the old values before any name is rebound.