Multiple assignment in Python: Assign multiple values or the same value to multiple variables
In Python, the =
operator is used to assign values to variables.
a = 100
b = 200
print(a)
# 100
print(b)
# 200
You can assign values to multiple variables in one line.
Assign multiple values to multiple variables
You can assign multiple values to multiple variables by separating them with commas ,
.
a, b = 100, 200
print(a)
# 100
print(b)
# 200
You can assign values to more than three variables, and it is also possible to assign values of different data types to those variables.
a, b, c = 0.1, 100, 'string'
print(a)
# 0.1
print(b)
# 100
print(c)
# string
When only one variable is on the left side, values on the right side are assigned as a tuple to that variable.
a = 100, 200
print(a)
print(type(a))
# (100, 200)
# <class 'tuple'>
If the number of variables on the left does not match the number of values on the right, a ValueError
occurs. You can assign the remaining values as a list by prefixing the variable name with *
.
# a, b = 100, 200, 300
# ValueError: too many values to unpack (expected 2)
# a, b, c = 100, 200
# ValueError: not enough values to unpack (expected 3, got 2)
a, *b = 100, 200, 300
print(a)
print(type(a))
# 100
# <class 'int'>
print(b)
print(type(b))
# [200, 300]
# <class 'list'>
*a, b = 100, 200, 300
print(a)
print(type(a))
# [100, 200]
# <class 'list'>
print(b)
print(type(b))
# 300
# <class 'int'>
For more information on using *
and assigning elements of a tuple and list to multiple variables, see the following article.
You can also swap the values of multiple variables in the same way. See the following article for details:
Assign the same value to multiple variables
You can assign the same value to multiple variables by using =
consecutively.
For example, this is useful when initializing multiple variables with the same value.
a = b = 100
print(a)
# 100
print(b)
# 100
After assigning the same value, you can assign a different value to one of these variables. As described later, be cautious when assigning mutable objects such as list
and dict
.
a = 200
print(a)
# 200
print(b)
# 100
You can apply the same method when assigning the same value to three or more variables.
a = b = c = 'string'
print(a)
# string
print(b)
# string
print(c)
# string
Be careful when assigning mutable objects such as list
and dict
.
If you use =
consecutively, the same object is assigned to all variables. Therefore, if you change the value of an element or add a new element in one variable, the changes will be reflected in the others as well.
a = b = [0, 1, 2]
print(a is b)
# True
a[0] = 100
print(a)
# [100, 1, 2]
print(b)
# [100, 1, 2]
b = [0, 1, 2]
a = b
print(a is b)
# True
a[0] = 100
print(a)
# [100, 1, 2]
print(b)
# [100, 1, 2]
If you want to handle mutable objects separately, you need to assign them individually.
after
c = []; d = []
,c
andd
are guaranteed to refer to two different, unique, newly created empty lists. (Note thatc = d = []
assigns the same object to bothc
andd
.) 3. Data model β Python 3.11.3 documentation
a = [0, 1, 2]
b = [0, 1, 2]
print(a is b)
# False
a[0] = 100
print(a)
# [100, 1, 2]
print(b)
# [0, 1, 2]
You can also use copy()
or deepcopy()
from the copy
module to make shallow and deep copies. See the following article.