[docs] bug

bob gailer bgailer at gmail.com
Sun Jan 27 16:07:14 CET 2013


On 1/25/2013 4:48 AM, ali mostafavi wrote:
> hi!
> i found a bug in python 2.7.3
> if we make a list like this:
> a = [[0]*3]*3
> then change the a[y][x] like this:
> a[1][1] = 1
> the whole column changes. the result would be:
> [[0,1,0],[0,1,0],[0,1,0]]

Please be very sure you found a bug before reporting one.
a = [[0]*3 # creates a list of 3 zeros
b = a*3 # is the equivalent of b = [a, a, a]
b[1][1] = 1 changes a[1] to 1

I presume you expected [[0,0,0],[0,1,0],[0,0,0]]

There is a VERY useful tool at http://www.pythontutor.com/visualize.html#

I entered my code so you can run it and see the results. Use the link below.

http://www.pythontutor.com/visualize.html#code=a+%3D+%5B0%5D*3%0Ab+%3D+%5Ba%5D*3%0Ab%5B1%5D%5B1%5D+%3D+1%0A&mode=display&cumulative=false&heapPrimitives=false&drawParentPointers=false&textReferences=false&py=2&curInstr=3

-- 
Bob Gailer
919-636-4239
Chapel Hill NC



More information about the docs mailing list