Skip to content

Multiple Assignment

diagram a = b = [] and a, b = [], [] are not the same statement mermaid
Chained assignment evaluates the right-hand side once and binds every name to that one object. With an immutable value nothing can go wrong. With a list, a dict or a set, both names now refer to the same container, so appending through one appears through the other.

We can assign multiple values to multiple variables in one line. This is called multiple assignment.

variable.py
# Multiple assignment
a, b, c = 10, 20.5, "Hello, World!"
print(a, b, c)

Output

command
C:\Users\Your Name> python variable.py
10 20.5 Hello, World!

We can assign one value to multiple variables in one line. This is called one value to multiple variables.

variable.py
# One value to multiple variables
a = b = c = 10
print(a, b, c)

Output

command
C:\Users\Your Name> python variable.py
10 10 10

We can assign multiple values to one variable in one line. This is called multiple values to one variable.

variable.py
# Multiple values to one variable
a = 10, 20, 30
print(a)

Output

command
C:\Users\Your Name> python variable.py
(10, 20, 30)

In this case, the variable a is a tuple.

We can unpack a tuple in multiple variables. This is called unpacking a tuple.

variable.py
# Unpacking a tuple
a = 10, 20, 30
x, y, z = a
print(x, y, z)

Output

command
C:\Users\Your Name> python variable.py
10 20 30

We can unpack a list in multiple variables. This is called unpacking a list.

variable.py
# Unpacking a list
a = [10, 20, 30]
x, y, z = a
print(x, y, z)

Output

command
C:\Users\Your Name> python variable.py
10 20 30

When you do not know (or do not care) how many items are in the middle, a starred variable * captures the rest as a list. This is called extended unpacking.

star_unpack.py
numbers = [1, 2, 3, 4, 5]
 
first, *rest = numbers
print(first, rest)        # 1 [2, 3, 4, 5]
 
first, *middle, last = numbers
print(first, middle, last)  # 1 [2, 3, 4] 5

Multiple assignment gives Python an elegant one-line swap — no temporary variable needed. The right-hand side is built into a tuple first, then unpacked.

swap.py
a, b = 5, 10
a, b = b, a
print(a, b)   # 10 5

sketch a = b = [] makes one list, not two p5.js
The right-hand side is evaluated once, and every name in the chain is bound to that single object. Append through one name and the other shows it, because there is nothing else to show. Compare it with the tuple-unpacking form, which evaluates the brackets twice and really does make two lists.
pch.quizTag pch.quizDefaultTitle
  1. `a = b = []` then `a.append(1)`. What is `b`?

    pch.quizShowAnswer

    B — `[1]` — The right-hand side is evaluated once and both names are bound to that single list. Measured: `a is b` is True. `x, y = [], []` creates two lists instead.

  2. Why is `i = j = 0` harmless while `a = b = []` is a trap?

    pch.quizShowAnswer

    B — `0` cannot be modified, so sharing it can never be observed — Sharing only matters when the shared object can change. Immutable values — numbers, strings, tuples — are safe to share because nothing can mutate them.

  3. `grid = [[0] * 3] * 3` then `grid[0][0] = 9`. What is `grid`?

    pch.quizShowAnswer

    B — `[[9,0,0], [9,0,0], [9,0,0]]` — Verified. `* 3` repeats the reference, not the row, so all three names point at one list. Build it with a comprehension instead: `[[0]*3 for _ in range(3)]`.

  4. Which statement creates two independent lists?

    pch.quizShowAnswer

    B — `a, b = [], []` — Only the tuple-unpacking form evaluates `[]` twice. The others all bind one object to two names.

Exercise 1 – Assign Same Value to Multiple Variables

Section titled “Exercise 1 – Assign Same Value to Multiple Variables”

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading