How to assign multiple values to a same variable in Python?


In Python, if you try to do something like

a = b = c = [0,3,5]
a[0] = 10

You'll end up with the same values in

a, b, and c: [10, 3, 5]

This is because all three variables here point to the same value. If you modify this value, you'll get the change reflected in all names, ie, a,b and c. To create a new object and assign it, you can use the copy module. 

 example

a = [0,3,5]
import copy
b = copy.deepcopy(a)
a[0] = 5

print(a)
print(b)

Output

This will give the output −

[5,3,5]
[0,3,5]

Updated on: 05-Mar-2020

799 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements