Because objects a
and b
reference the same object (as you have observed by checking both objects' id
s). If you change a value in one, the other will change as well. It's like b
is a clone of a
, but will continue being a clone.
To avoid this behaviour, you can do:
b = a[:]
Which assigns b
a copy of a
.
Or:
b = list(a)
Or:
import copy
b = copy.copy(a) # The copy module can be useful for nested lists.