[Tutor] python 2D list
Yuanxin Xi
yxi at bcm.edu
Fri May 23 23:22:23 CEST 2008
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
More information about the Tutor
mailing list