I'm trying to teach myself python (and programming in general) and am a little confused about variable assignment. I understand that if I have
>>> a = [1,2,3]
>>> b = a
that b
refers to the same object in memory as a
does. So if I wanted to create a new list, b
, with the same values as a
currently has, how would I achieve that?
Also, consider this example:
>>> a = [1, 2, 3]
>>> b = a
>>> x = a[1]
>>> a[1] = 4
>>> print a, b, x
[1, 4, 3] [1, 4, 3] 2
I see from this example, that x
is a new object but b
points to a
. Could someone explain to me what is going on here, why x
is a new object but b
isn't?