Last modified: Feb 15, 2023 By Alexander Williams

2 Methods to Set two Variables to the Same Value in python

In python, multiple methods exist to set two variables to the same value.

In this tutorial, we'll learn the two best of these methods, which are the "=" operator , copy() , and deepcopy() method. In addition, we'll see the difference between these methods.

Using "=" operator

We can use the = operator to assign Value to two Variables:

#  assign Value to 2 Variables
a = b = "Hello world"

# Print a
print(a)

# Print b
print(b)

output:

Hello world
Hello world

Now, let's print the identity of each variable:

# Print a ID
print(id(a))

# Print b ID
print(id(b))
140243415629360
140243415629360

As you can see, the both variables have the same identity. Does that mean if we change the value of one, the other will be changed?

The answer is Yes and No!

Let me prove it:

#  assign String Value to 2 Variables
a = b = "Hello world"

# Change Value of "a" variable
a = "Hello Python"

# Print a
print(a)

# Print b
print(b)

Output:

Hello Python
Hello world

Here the string's value of both variables is not changed. Now let's do the same thing with List.

# assign List to 2 Variables
list_a = list_b = [1, 2, 3]

# Change element value [0] of list_a
list_a[0] = 7

print(list_a)

print(list_b)

output:

[7, 2, 3]
[7, 2, 3]

Perfect! the list of both variables has been changed! The same goes for dictionary .

# assign dict to 2 Variables
dict_a = dict_b = {"first_name":"Mark", "last_name":"Seo", "Age":15}

# Change first_name of dict_a
dict_a['first_name'] = "Leo"

print(dict_a)

print(dict_b)

output:

{'first_name': 'Leo', 'last_name': 'Seo', 'Age': 15}
{'first_name': 'Leo', 'last_name': 'Seo', 'Age': 15}

Using copy()

Copy () is a method that returns a shallow copy. This method accepts list sets and dictionaries.

However, we can use copy() to set the same list to 2 variables.

import copy

# list_a
list_a = [1, 2, 3]

# Copy
list_b = copy.copy(list_a)

print(list_a)
## Output: [1, 2, 3]

print(list_b)
## Output: [1, 2, 3]

Print identity of each variable:

print(id(list_a))
# output: 140439020011200

print(id(list_b))
# output: 140439020011136

Here we have a different identity.

Unlike the "=" operator method, with the copy() , the change will not affect the other variables.

import copy

# list
list_a = [1, 2, 3]

# Copy
list_b = copy.copy(list_a)

# Change Value of the element
list_a[0] = 15

print(list_a)
## Output: [15, 2, 3]

print(list_b)
## Output: [1, 2, 3]

As you can see list_b is not changed.

Using deepcopy()

deepcopy() is a method that returns a deep copy of a list, set, or dictionary. We can also use this method to set the same value to 2 variables.

import copy

# list
list_a = [1, 2, 3]

# deepcopy()
list_b = copy.deepcopy(list_a)

# Change Value of the element
list_a[0] = 15

print(list_a)
## Output: [15, 2, 3]

print(list_b)
## Output: [1, 2, 3]

As you can see, we got the same output as the copy() method. Now, the question is:

What is the difference between all of these methods?

The difference between "=", copy() and deepcopy()

To understand the direfence "=" operator, copy() and deepcopy() , let's see an example:

import copy

list_root = [1, 2, [3, 4]]

# list_root
print("list_root:", list_root)

# Equal operator
eq_list = list_root

# copy()
cp_list = copy.copy(list_root)

# deepcopy()
dp_list = copy.deepcopy(list_root)


# Change value of elements of list_root
list_root[1] = 400 # Item
list_root[2][0] = 800 # Nested Item

# Equal method
print("Equal operator:", eq_list)

# Copy() method
print("copy():", cp_list)

# Deepcopy() method
print("deepcopy():", dp_list)

Output:

Equal operator: [1, 400, [800, 4]]
copy(): [1, 2, [800, 4]]
deepcopy(): [1, 2, [3, 4]]

I'm sure you understand. If you do not understand, let me exmplain.

After changing the original list list_root :

  • The list set by the equal operator is affected .
  • The list set by copy() is not affected except for nested elements .
  • The list set by deepcopy() is not affected at all .

Conclusion

In conclusion, we've learned three methods to set the same value to two variables. Also, you can use these methods, so set more than two variables.

I hope this article helps you.

Happy Coding ♥