Bug or not ?

Fredrik Lundh fredrik at pythonware.com
Fri May 4 14:14:14 EDT 2001


Changsen Xu wrote:
> >>> x = [ [0]*2 ] * 2
> >>> x
> [[0, 0], [0, 0]]
> >>> x[0][0] =1
> >>> x
> [[1, 0], [1, 0]]         ## This result differ from above
>
>
> Anybody can give me an explanation ? Thanks in advance.
> I was driven crazy to check my how-can-it-be-wrong
> tiny program dozens of times until I finally found the above
> difference!

did you remember to check the FAQ?

    http://www.python.org/doc/FAQ.html#4.50

    Q. My multidimensional list (array) is broken! What gives?

    A. You probably tried to make a multidimensional array like
    this:

       A = [[None] * 2] * 3

(see the webpage for the full story)

Cheers /F





More information about the Python-list mailing list