Identical lists aren't equivalent when compared is
unless they reference the same list. Just because the lists have the same values doesn't mean they reference the same list in memory.
E.g.,
>>> a = [1,2,3]
>>> id(a) # memory location list a references
161267628
>>> b = [1,2,3]
>>> id(b) # memory location list b references, a different memory location than list a
161276396
>>> a == b
True
>>> a is b
False
>>> c = a # by this method of assignment; c points to the same point that a does;
# hence changes to a and changes to c, will change both.
>>> id(c) # memory location that c references; this is the same as where a points.
161267628
>>> a is c
True
>>> c.append(4)
>>> print a
[1, 2, 3, 4]
>>> print b
[1, 2, 3]
>>> print c
[1, 2, 3, 4]
>>> d = a[:] # this just copies the values in a to d.
>>> id(d)
161277036
This makes sense that they point to different memory locations, because if you may want to say change the first list (like append 4
to the end a
) without modifying b
which wouldn't be possible if a
and b
pointed to the same location in memory.