Python Multiple Assignment Guide: Swap, Unpack & Use Extended Unpacking

Python Multiple Assignment, unpacking and chained assignment are techniques to assign multiple variables in a single line. These approaches let you work with several values together in a concise and readable way, making code cleaner and more efficient.

Note: These techniques appear later in the Python Variables Roadmap, after the core variable concepts are firmly established.

Let’s explore these multiple assignment techniques in Python with clear explanations and beginner-friendly examples.

1. What Is Python Multiple Assignment?

Multiple assignment in Python refers to assigning values to multiple variables in a single statement.

x, y, z = 1, 2, 3
# x = 1, y = 2, z = 3

This simplifies your code and reduces repetitive assignments.

2. Assigning Multiple Values to Multiple Variables in Python

Each value on the right-hand side is assigned to the corresponding variable on the left-hand side in order.

var1, var2, var3 = value1, value2, value3
x, y, z = 1, 2, 3
print(x)  # Output: 1
print(y)  # Output: 2
print(z)  # Output: 3

3. Assigning the Same Value to Multiple Python Variables

All the variables point to the same value.

a = b = c = 100
print(a, b, c)  # Output: 100 100 100
# All three variables now refer to the same value in memory.

4. Swap Python Variable Values (Without Temp Variable)

Python allows swapping values between variables directly using multiple assignment.

x = 5
y = 10

x, y = y, x
print(x, y)  # Output: 10 5
# This is a clean and elegant way to swap values without a temporary variable.

5. Unpack a Python List or Tuple into Variables

Multiple assignment can be used to unpack the contents of a list, tuple, or other iterable.

# Example (Tuple)
data = (7, 8, 9)
a, b, c = data
print(a, b, c)  # Output: 7 8 9

# Example (List)
values = ["Python", "Java", "C++"]
lang1, lang2, lang3 = values
print(lang1)  # Output: Python

# Number of variables must match the number of elements in the sequence.

6. Using * for Extended Unpacking in Python

When the number of values is unknown or variable, Python allows using * to collect excess values into a list.

first, *middle, last = [10, 20, 30, 40, 50]
print(first)   # Output: 10
print(middle)  # Output: [20, 30, 40]
print(last)    # Output: 50
# This is known as extended unpacking.

7. Ignoring Unused Variables Using the Underscore (_) _

Sometimes, only a few values from a multiple assignment are needed. The underscore _ can be used as a throwaway variable.

x, _, y = (1, 2, 3)
print(x, y)  # Output: 1 3
# The middle value is ignored.

8. Multiple Assignment with Function Return Values in Python

When a function returns multiple values (e.g., tuple), multiple assignment is a clean way to capture them.

def get_position():
    return (100, 200)

x, y = get_position()
print(x, y)  # Output: 100 200

9. Python Chained Assignment: Assign Multiple Variables in One Line

Chained assignment can be combined with expressions.

a = b = c = 2 + 3
print(a, b, c)  # Output: 5 5 5
# All variables get the result of the expression 2 + 3.

10. Real-Life Examples of Python Multiple Assignment

# Assigning coordinates (latitude, longitude)
latitude, longitude = (27.700769, 85.300140)
print("Location:", latitude, longitude)

# Assigning same default value to multiple settings
debug, verbose, test_mode = False, False, False

# Ignoring password from user info
username, _, email = ("john_doe", "1234secret", "john@example.com")

11. Key Notes on Python Multiple Assignment

  • Number of variables must match the number of values unless using *.
  • Python allows using chained assignment and unpacking together.
  • Extended unpacking (*) only works in Python 3.x.

12. Summary Table: Python Multiple Assignment in Python

Type Example Description
Multiple values to variables x, y = 1, 2 Each value assigned in order
Same value to multiple variables a = b = c = 0 All variables get same value
Swapping variables x, y = y, x Swap without temp variable
Unpacking list/tuple a, b, c = [1, 2, 3] Unpacks each element
Extended unpacking x, *y, z = [1, 2, 3, 4] *y collects middle values
Ignoring values a, _, b = (1, 2, 3) _ used to ignore value
Function return unpacking x, y = get_coords() Captures multiple return values

Conclusion: Python Multiple Assignment

Multiple assignment in Python is a versatile feature that simplifies code and improves readability. It’s widely used in data unpacking, value swapping, and function handling, making it a foundational concept in Python programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top