Python Central HubSign in

Python Programming / Python Variables

Multiple Assignment

How to assign multiple values to multiple variables in Python. Learn about multiple assignment, one value to multiple variables, multiple values to one variable, and unpacking a tuple or list.

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.

Many Values to Multiple Variables

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

Example:

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

Output

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

One Value to Multiple Variables

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

Example:

Python
# One value to multiple variables
a = b = c = 10
print(a, b, c)

Output

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

Multiple Values to One Variable

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

Example:

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

Output

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

In this case, the variable a is a tuple.

Unpacking a Tuple

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

Example:

Python
# Unpacking a tuple
a = 10, 20, 30
x, y, z = a
print(x, y, z)

Output

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

Unpacking a List

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

Example:

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

Output

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

Extended Unpacking with *

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.

Python
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

Swapping Variables

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.

Python
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.

Check yourself

quizCheck yourself
  1. `a = b = []` then `a.append(1)`. What is `b`?

    Show answer

    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?

    Show answer

    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`?

    Show answer

    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?

    Show answer

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

Try it: Multiple Assignment Exercises

Exercise 1 – Assign Same Value to Multiple Variables

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Tuple Unpacking

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Assign from a List

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did