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

PythonProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

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 05:40:07

Advertisements