interesting thing about python list initializing

Viewed 92

I'm quiet confused to get this with python as below

>>> a = [[]]*3
>>> c=[[],[],[]]
>>> a
[[], [], []]
>>> c
[[], [], []]
>>> a == c
True
>>> a[1].append(2)
>>> a
[[2], [2], [2]]
>>> c[1].append(2)
>>> c
[[], [2], []]

I guess the reason is that in variable a, all three lists ponit to the same memory area until they are used in different ways. Is it right? Should I always be careful when doing initialization using things like * operator? THANKS!

3 Answers
Related