This question already has an answer here:
- List of lists changes reflected across sublists unexpectedly 12 answers
Can anyone explain why is this happening?
Case 1:
>>> a = [[0]] *3
>>> print a
[[0], [0], [0]]
>>> a[1].append(0)
>>> print a
[[0, 0], [0, 0], [0, 0]]
Case 2:
>>> a = [[0],[0],[0]]
>>> print a
[[0], [0], [0]]
>>> a[1].append(0)
>>> print a
[[0], [0, 0], [0]]
Why is this going on? I am expecting that the behavior of the array in case 1 to be as in case 2 but it is not for some reason.