Multi-dimensional list initialization trouble

jonkje at gmail.com jonkje at gmail.com
Thu May 25 17:22:50 EDT 2006


Hello I found this very strange; is it a bug, is it a "feature", am I
being naughty or what?

>>> foo = [[0, 0], [0, 0]]
>>> baz = [ [0]*2 ] * 2
>>> foo
[[0, 0], [0, 0]]
>>> baz
[[0, 0], [0, 0]]
>>> foo[0][0]=1
>>> baz[0][0]=1
>>> foo
[[1, 0], [0, 0]]
>>> baz
[[1, 0], [1, 0]]

Why on earth does foo and baz behave differently??

Btw.:
Python 2.4.1 (#1, Apr 10 2005, 22:30:36)
[GCC 3.3.5] on linux2

--- Jon Øyvind




More information about the Python-list mailing list