Why do variables containing lists in Python act differently from say variable containing integers in terms of storing/pointing towards values?

Viewed 12

I'm just confused with the behaviour of variables when storing lists. If we say x = y, for some reason changing y also changes x. This is not the case, however, when a variable is storing an integer.

For integers:

spam = 1
cheese = spam
spam = 2
print(cheese)
>>> 1

For lists:

spam = [1,2,3]
cheese = spam
spam.remove(1)
print(cheese)
>>> [2,3]
0 Answers
Related