
Table of Contents(目次)
What is Python Unpacking (Multiple Assignment)?
In this lecture, we will learn the operation called unpacking.
Unpacking is the process of 'extracting multiple values from a data structure (such as a list or tuple) at once and assigning them to multiple variables.' It is useful when handling related data such as dates or coordinates.
How to Unpack Lists and Tuples
You can assign elements of a list or tuple to variables in order. By writing year, month, day = [2025, 10, 5]
, the variable year receives 2025, month receives 10, and day receives 5.
Lists and tuples require managing data by index, which can make it difficult to understand which data is at which index. By giving descriptive variable names and assigning data obtained from lists or tuples to each variable, the code becomes easier to read and maintain.
One thing to note when using unpacking is that the number of elements in the list/tuple must match the number of variables; otherwise, an error will occur.
# Year/Month/Day
year, month, day = [2025, 10, 5]
print(year, month, day) # 2025 10 5
# Example of splitting a string like 'Year-Month-Day' into elements
date_str = '2025-09-30'
year, month, day = date_str.split('-') # split the string by '-' → ['2025','09','30']
print(year, month, day) # '2025' '09' '30' (convert to int() if numerical values are needed)
# Working with coordinates
coordinates = (35.6895, 139.6917) # Latitude and longitude of Tokyo
lat, lon = coordinates
print('Latitude:', lat) # Latitude: 35.6895
print('Longitude:', lon) # Longitude: 139.6917
Unpacking Using the Asterisk (*)
By writing *var
, the remaining elements are assigned to that variable as a list.
As mentioned earlier, you need to match the number of elements with the number of variables, but by adding an asterisk (*
) to one of the variables, the remaining elements are converted into a list and assigned to that variable.
For example, suppose you only want the first and last elements from a list with many elements. You can write first, *middle, last = numbers
, which assigns the first element to first, the last element to last, and the remaining elements as a list to middle.
numbers = [10, 20, 30, 40, 50]
first, *middle, last = numbers
print(first) # 10
print(middle) # [20, 30, 40]
print(last) # 50
One thing to note here is that 'the number of elements in the list/tuple must be at least equal to the number of variables without an asterisk.' The variables without an asterisk must each receive a value from the list/tuple. The variable with an asterisk can receive zero elements without any problem (in that case, an empty list is assigned).
# OK example: There are at least as many elements as variables without an asterisk
numbers = [10, 20]
first, *middle, last = numbers
print(first) # 10
print(middle) # [] (It is fine if middle receives zero elements)
print(last) # 20
# NG example: Not enough elements in the list
# (There is no element to assign to the variable last)
numbers = [10]
first, *middle, last = numbers
Notes on Unpacking Sets
Sets can also be unpacked. However, because sets do not guarantee order, you cannot know which variable will receive which value until runtime.
a, b, c = {100, 200, 300}
print(a, b, c) # 200 100 300
Unpacking Dictionaries (keys, values, and items)
Unpacking a dictionary as-is assigns each key to a variable.
You can also unpack the values using the values()
method to retrieve each value.
Using the items()
method allows you to unpack each (key, value)
pair.
※ As explained in the dictionary lecture, the order of elements is only guaranteed in Python 3.7 and later. For earlier versions, you must write code assuming the order may vary.
profile = {'name': 'Nico', 'age': 10}
# Each key is assigned
key1, key2 = profile
print('key1:', key1) # key1: name
print('key2:', key2) # key2: age
# Unpack the values
name, age = profile.values() # Order guarantee applies only to Python 3.7 and later
print('Name:', name) # Name: Nico
print('Age:', age) # Age: 10
# Unpack the (key, value) pairs
first_pair, second_pair = profile.items()
print(first_pair) # ('name', 'Nico')
print(second_pair) # ('age', 10)