Lists & two dimensions

Chris Liechti cliechti at gmx.net
Sat Jul 20 10:35:01 EDT 2002


"Duncan Smith" <buzzard at urubu.freeserve.co.uk> wrote in 
news:ahbqkn$gn3$1 at newsg1.svr.pol.co.uk:

>>>> b = [[-1] * 3 ]
>>>> b
> [[-1, -1, -1]]                  # a list containing a list
>>>> b * 3
> [[-1, -1, -1], [-1, -1, -1], [-1, -1, -1]]
> 

or better:
b = [[-1]*3 for i in range(3)]

otherwise the 3 lists are references to a single list and when you modify 
one, the other two change too (as they are one list...)

anyway for a flat list of 9 elements i would use
b = [-1]*(3*3)

chris

-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list