Python lets you assign values to several variables in one line instead of writing a separate statement for each one. This feature, often called multiple assignment or tuple unpacking, reduces visual noise and reads naturally once you know the pattern. It also powers one of Python's more elegant tricks: swapping two values without a temporary variable.
The mechanism is simple. Python evaluates every value on the right side first, packs them into a tuple, and then unpacks that tuple into the names on the left, in order. If you have followed the earlier articles on Python variables and reassignment, multiple assignment is the natural next step.
Basic multiple assignment
The simplest form puts one name per value, with commas separating both sides:
x, y, z = 10, 20, 30
print(x, y, z) # 10 20 30Python builds a tuple from the right side, then assigns its first item to x, the second to y, and the third to z. The number of names on the left must match the number of values on the right; a mismatch raises a ValueError at runtime, which catches bugs where you accidentally provided the wrong number of values instead of letting them pass silently.
Swapping variables without a temporary variable
In many languages, swapping two values needs a temporary variable to hold one value while the other moves. Python's multiple assignment collapses that into a single line:
a, b = b, aPython evaluates the right side first, reading the current values of both names and packing them into a tuple before any assignment happens. Only after that is the left side unpacked, so the original values are captured before either name is overwritten. The same idea extends to more than two variables. Writing a, b, c = c, a, b rotates three values in one line, since the entire right side is still evaluated before any name on the left changes.
Chain assignment and unpacking any iterable
If several variables should start with the same value, chain the assignment: width = height = depth = 0 gives all three names the same object. This is safe for immutable values like numbers, since reassigning one name later does not affect the others. For a mutable value like a list, chaining creates one shared list, so modifying it through any one name is visible through all of them, which is rarely what you want; create separate lists instead if they need to stay independent.
Multiple assignment is not limited to literal values. Any iterable, including a function's return value, can be unpacked directly into names:
quotient, remainder = divmod(17, 3) # 5, 2
scores = {"alex": 92, "jordan": 85}
for name, score in scores.items():
print(f"{name} scored {score}")Unpacking a function's return tuple directly into named variables makes the calling code self-documenting, since you read the meaning of each value at the point of assignment instead of indexing into a tuple afterward. The same pattern shows up constantly in loops, where the built-in enumerate function returns index-value pairs that unpack directly into two loop variables instead of requiring you to subscript a tuple inside the loop body.
Starred expressions for flexible unpacking
Sometimes you do not know exactly how many values an iterable holds, or you want part of it while unpacking the rest. A starred name, prefixed with an asterisk, captures any number of remaining values into a list:
first, *rest = [1, 2, 3, 4, 5]
print(first) # 1
print(rest) # [2, 3, 4, 5]The starred name can appear at the start, middle, or end of the left side, and it always collects whatever values are left over after the plain names are assigned; if nothing is left, it collects an empty list. Only one starred name is allowed per assignment, since Python needs an unambiguous way to decide which values belong to which name. This pattern reads especially well when splitting a file path into its directory portion and its final filename, where everything except the last piece belongs together.
Where multiple assignment fits
Multiple assignment is one application of Python's broader tuple packing and unpacking mechanism, which also appears in function arguments, return values, and exception handling. Recognizing that connection now will make those later topics feel familiar rather than new.
You now know how to create, name, reassign, and unpack variables into multiple names at once. Revisit creating and assigning variables any time one of these patterns feels unfamiliar, since every technique here builds on that same foundation.
Rune AI
Key Insights
- Assign several variables in one line with a comma-separated list on each side.
- Swap values without a temporary variable using a, b = b, a.
- Starred expressions capture remaining values during unpacking into a list.
- Chain assignment with a = b = c = 0 to give several names the same value.
- Multiple assignment is powered by tuple packing and unpacking.
Frequently Asked Questions
How do you swap two variables in Python without a temporary variable?
What happens if the number of variables does not match the number of values?
Can I assign the same value to multiple variables at once?
Conclusion
Multiple assignment and tuple unpacking reduce boilerplate and make code more expressive. Use them to swap values, capture multiple return values, and iterate cleanly.
More in this topic
Create and Assign Variables in Python
Learn every way to create and assign variables in Python, from basic single assignment to expressions, type hints, and practical patterns beginners use every day.
Python Variable Naming Rules
Learn Python's variable naming rules, including allowed characters, reserved keywords, PEP 8 conventions, and how to choose names that make your code readable.
Python Variables Explained
Understand what Python variables are, how they store and reference data, and why Python's variable model is different from other programming languages.