Module 3 · Variables and Data Types — topic 14 of 15
Multiple assignment
Assign several variables in one statement with tuple unpacking, swap values without a temporary variable, unpack sequences, and collect the rest with the star operator.
Learning objectives
- Assign several names in one statement
- Swap two variables in one line
- Unpack sequences, including with *
Definition
Assign several variables in one statement with tuple unpacking, swap values without a temporary variable, unpack sequences, and collect the rest with the star operator.
Introduction
Python lets you assign several names at once by listing them on the left of =. The values on the right are matched up by position. This is called unpacking, and it makes many small tasks neater.
Why it matters
Unpacking appears everywhere in idiomatic Python: swapping values, reading pairs from dictionaries, splitting a line of input into fields, and receiving several results from one function.
Detailed explanation
x, y = 3, 4 assigns 3 to x and 4 to y. The right side is built first as a tuple, then unpacked into the names on the left.
Because the right side is evaluated completely before assignment, a, b = b, a swaps two values safely.
Any sequence can be unpacked: a list, a tuple or a string of the right length. name, age = ["Ravi", 21] works.
The number of names must match the number of values, or Python raises ValueError: too many values to unpack (or not enough).
A starred name collects the leftovers into a list: first, *rest = [1, 2, 3, 4] gives first = 1 and rest = [2, 3, 4].
Use _ as a name for values you intend to ignore: name, _, city = record.
Unpacking works in for loops too. When you loop over a list of pairs such as [("Asha", 82), ("Ben", 74)], writing for name, mark in results: unpacks each pair into two clear names on every iteration, which is far more readable than results[i][0] and results[i][1].
Syntax
- Names and values are matched by position.
- Swapping needs no temporary variable.
- *name gathers the remaining values into a list.
- _ is a convention for “ignore this value”.
Code examples
Several names at once
Ravi 21 Kochi
Swapping
9 5
More examples
Unpacking records
Meena 22 ['Bengaluru', 'Data Science'] highest: 91 lowest: 60 ValueError: too many values to unpack (expected 2, got 3)
Step-by-step explanation
- split(",") produces four strings; name and age take the first two and details collects the rest.
- sorted(..., reverse=True) orders the scores from high to low.
- highest takes the first, lowest the last, and *_ ignores everything in between.
- Unpacking three values into two names raises ValueError with a precise message.
Expected output
Running “Unpacking records” prints exactly:
Practical applications
- Loops over dictionary items unpack each pair: for key, value in data.items().
- Functions return several results that callers unpack: minimum, maximum = bounds(values).
- Parsing CSV lines unpacks columns into named variables.
Best practices
- Use unpacking to give each part of a record a clear name.
- Use a, b = b, a for swaps.
- Use _ for values you don’t need.
Common mistakes
Mismatched counts
Avoid
Prefer
The number of names must equal the number of values unless you use *.
Swapping with a temporary in two steps
Avoid
Prefer
The two-step version overwrites a before saving it.
Key takeaways
- Multiple assignment matches names to values by position.
- a, b = b, a swaps safely.
- *name collects remaining values; mismatched counts raise ValueError.
Try it yourself
- 1Unpack the date string "22-11-2026" into day, month and year variables and print them in year-month-day order.
- 2Given a list of exam marks, unpack the top mark and the rest into two variables.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?