Assign Multiple Values
Learn how to assign multiple values in Python: tuple unpacking, one value to many variables, starred assignment, and common gotchas with examples.
Assign Multiple Values in Python
Python lets you assign values to several variables in a single statement. This covers four common patterns you will encounter in real code:
- Multiple assignment — different values to different variables in one line.
- Chained assignment — the same value to several variables at once.
- Iterable unpacking — pull items out of a list, tuple, or any iterable.
- Starred assignment — collect a variable-length "rest" into a list.
Each pattern is explained below with runnable examples and the errors to watch out for.
Multiple Assignment (Tuple Unpacking)
The most common form uses a comma-separated list of names on the left and a matching list of values on the right:
variable1, variable2, variable3 = value1, value2, value3Python packages the right-hand side into a temporary tuple and then unpacks it into the names on the left, left to right. The number of names and values must match exactly; if they do not, Python raises a ValueError.
Assigning three variables of different types in one line:
Output:
Python
3.7
TrueThis is equivalent to writing three separate assignment statements, but is more concise and makes the relationship between the names obvious at a glance.
Swapping Variables
Because Python evaluates the entire right-hand side before any assignment happens, you can swap two variables without a temporary:
x = 5
y = 10
# Before swapping
print("Before swapping")
print("x =", x)
print("y =", y)
# Swap in one line — no temp variable needed
x, y = y, x
# After swapping
print("After swapping")
print("x =", x)
print("y =", y)Output:
Before swapping
x = 5
y = 10
After swapping
x = 10
y = 5In most other languages this swap requires a third temporary variable. Python's tuple-packing semantics make the one-liner safe.
ValueError: Count Mismatch
If the number of values does not match the number of names, Python raises a ValueError immediately:
a, b = 1, 2, 3 # too many values
# ValueError: too many values to unpack (expected 2)
a, b, c = 1, 2 # too few values
# ValueError: not enough values to unpack (expected 3, got 2)When you see this error, count the names on the left and the values on the right — they must be equal, or you need starred assignment (see below).
Chained Assignment (One Value, Many Variables)
To give several variables the same initial value, chain the assignment operators:
x = y = z = 0
print(x, y, z)Output:
0 0 0Python evaluates 0 once and binds all three names to the same object. This works correctly for immutable objects (integers, strings, tuples). Be careful with mutable defaults — all three names point to the same list object:
# Pitfall: shared mutable object
a = b = c = []
a.append(1)
print(b) # [1] — b is the same list, not a copy!If you need independent lists, use a list comprehension or separate assignments:
a, b, c = [], [], []Unpacking an Iterable
You can unpack any iterable — a list, tuple, string, range, or other — directly into variables:
a, b, c = [10, 20, 30]
print(a, b, c)Output:
10 20 30This is the same mechanism as multiple assignment; Python does not care whether the right-hand side is a literal tuple or a list variable. For example, unpacking a tuple works identically:
coords = (51.5, -0.1)
lat, lon = coords
print(lat) # 51.5
print(lon) # -0.1See the Unpack Tuples chapter for a deeper look at this pattern.
Starred Assignment (Extended Unpacking)
When you do not know how many items an iterable contains, prefix one name with * to collect the remaining items into a list:
first, *rest = [10, 20, 30, 40]
print(first) # 10
print(rest) # [20, 30, 40]The starred name can appear anywhere in the target list — at the start, middle, or end:
first, *middle, last = [10, 20, 30, 40]
print(first) # 10
print(middle) # [20, 30]
print(last) # 40*head, second_last, last = range(1, 6)
print(head) # [1, 2, 3]
print(second_last) # 4
print(last) # 5Rules for starred assignment:
- Only one starred name is allowed per statement.
- The starred name always collects a
list, even when it matches zero or one items. - All non-starred names must match exactly one item.
When to Use Each Pattern
| Pattern | Typical use case |
|---|---|
a, b = 1, 2 | Return multiple values from a function; swap two variables |
x = y = z = 0 | Initialize a group of counters or flags to the same value |
a, b, c = my_list | Unpack a fixed-length sequence into meaningful names |
first, *rest = items | Separate head from tail in a list of unknown length |
Related Topics
- Python Variables — how variables work and naming rules
- Variable Names — naming conventions (snake_case, keywords)
- Unpack Tuples — detailed coverage of tuple unpacking
- Python Lists — list fundamentals referenced in examples above
- Global Variables — scope and the
globalkeyword