I'm a Python newbie and still exploring, just surprised to see this 2D list assignment while debugging.

>>> a = [[0] *3] *4
>>> a
[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> a[1][2] = 1
>>> a
[[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 0, 1]]

why is that a[0][2] a[1][2] a[2][2] a[3][2] are all assigned to 1 instead of only a[1][2] = 1? This is a little confusing and inconsistent with almost all other languages. Could anyone please help explain the assignment mechanism here? Thanks
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to