Assign Multiple Variables in Python

By James L.

In this article, we will discuss the following topics:

  • Assign the same value to multiple variables
  • Assign multiple values to multiple variables (Tuple Unpacking)

Assign the same value to multiple variables

We can assign the same value to multiple variables in Python by using the assignment operator (=) consecutively between the variable names and assigning a single value at the end.

For example:

a = b = c = 200

The above code is equivalent to 

c = 200

b = c

a = b

In the above example, all three variables a, b, and c are assigned a value of 200.

If we have assigned the same value to multiple variables in one line, then we must be very careful when modifying or updating any of the variables.

Lots of beginners get this wrong. Keep in mind that everything in Python is an object and some objects in Python are mutable and some are immutable.

Immutable objects are objects whose value cannot be changed or modified after they have been assigned a value.

Mutable objects are objects whose value can be changed or modified even after they have been assigned a value.

For immutable objects like numeric types (int, float, bool, and complex), str, and tuple, if we update the value of any of the variables, it will not affect others.

For example:

a = b = c = 200
b = 500
print(a)
print(b)
print(c)

Output:

200
500
200

In the above example, I initially assigned a value of 200 to all three variables a, b, and c. Later I changed the value of variable b to 500. When I printed all three variables, we can see that only the value of variable b is changed to 500 while the values of variables a and c are still 200.

Note: In Python, immutable data types and immutable objects are the same. Also, mutable data types and mutable objects are the same. This may not be the case in other programming languages.

For mutable objects like list, dict, and set, if we update the value of any one of the variables. The value of other variables also gets changed.

For example:

a = b = c = [1, 2, 3, 4]

b.append(10)

print(a)
print(b)
print(c)

Output:

[1, 2, 3, 4, 10]
[1, 2, 3, 4, 10]
[1, 2, 3, 4, 10]

In the above example, I initially assigned [1, 2, 3, 4] to all three lists a, b, and c. Later I appended a value of 10 to the list b. When I print all three lists to the console, we can see that the value of 10 is not just appended to list b, it is appended to all three lists a, b, and c.

Assigning the same value of mutable data types to multiple variables in a single line may not be a good idea. The whole purpose of having a mutable object in Python is so that we can update the values. 

If you are sure that you won’t be updating the values of mutable data types, then you can assign values of mutable data types to multiple variables in a single line. Otherwise, you should assign them separately.

For example:

a = [1, 2, 3, 4]

b = [1, 2, 3, 4]

c = [1, 2, 3, 4]

The value of immutable objects cannot be changed doesn’t mean the variable assigned with an immutable object cannot be reassigned to a different value. It simply means the value cannot be changed but the same variable can be assigned a new value. 

We can see this behavior of an object by printing the id of the object by using the id() function. The id is the memory address of the object. In Python, all objects have a unique id.

The ID of immutable object:

a = 100
print(id(a))  # Output: 1686573813072

a = 200
print(id(a))  # Output: 1686573816272

In the above example, I initially assigned a value of 100 to variable a. When I printed the id of variable a to the console, it is 1686573813072. Later I changed the value of a to 200. Now when I print the id of variable a, it is 1686573816272. Since the id of variable a in the beginning and after I changed the value is different, we can conclude that variable a was pointing to a different memory address in the beginning and now it is pointing to a different memory address. It means that the previous object with its value is replaced by the new object with its new value.

Note: The id will be different from my output id for your machine. May even change every time you run the program.

The ID of mutable object:

a = [1, 2, 3, 4]
print(id(a))  # Output: 1767333244416

a.append(10)
print(id(a))  # Output: 1767333244416

In the above example, I have assigned [1, 2, 3, 4] to list a. When I printed the id of list a, it is 1767333244416. Later, I appended a value of 10 to the list a. When I print the id of the list a, it is still 1767333244416. Since the id of the list a in the beginning and after I appended a value is the same, we can conclude that list a is still pointing to the same memory address. This means that the value of list a is modified. 

Assign multiple variables to the multiple values (Tuple Unpacking)

In Python, we can assign multiple values separated by commas to multiple variables separated by commas in one line using the assignment operator (=).

For example:

a, b, c = 100, 200, 300

In the above example, variable a is assigned a value of 100, variable b is assigned a value of 200, and variable c is assigned a value of 300 respectively.

We have to make sure that the number of variables is equal to the number of values. Otherwise, it will throw an error.

We can also assign values of different data types to multiple variables in one line.

a, b, c = 2.5, 400, "Hello World"

In the above example, variable a is assigned a value of float data type, variable b is assigned a value of integer data type, and c is assigned a value of string data type.

If the number of values is more than the number of variables, we can still assign those values to multiple variables by placing an asterisk (*) before the variable.

For example:

a, *b, c = 1, 2, 3, 4, 5
print(a)
print(b)
print(c)

Output:

1
[2, 3, 4]
5

Notice the variable with an asterisk (*) is assigned a list.

This method of assigning multiple variables to multiple values is also called tuple unpacking. In the above examples, what we are doing is, we are unpacking the elements of a tuple and assigning them to multiple separate variables.

In Python, we can create tuples with or without parenthesis.

E.g. a = 1, 2, 3 and a = (1, 2, 3) both are the same thing.

Our first example of assigning multiple variables to multiple values can also be written as:

my_tuple = (100, 200, 300)

a, b, c = my_tuple
print(a)  # Output: 100
print(b)  # Output: 200
print(c)  # Output: 300

We can also assign tuples to multiple variables directly.

a, b, c = (100, 200, 300)

We can also unpack other iterable objects like lists, sets, and dictionaries.