List of lists: caveat?

George Thomas george at cc.gatech.edu
Sat Mar 9 15:09:30 EST 2002


Hi everyone,
I just tried creating a 2D array the 'list-of-lists' way as follows:
two_d_array = [ [0] * 20] * 10 # Creates a 10 * 20 array  -- (I)

two_d_array[0][3] = 1 # Sets element [0][3] to 1

I expected nothing unnerving, except that
print two_d_array[1][3]
gives me 1
as does print two_d_array[any valid index from 0 to 9][3]

What are the exact semantics of the line marked (I) above? If there is a 
caveat, is there any way I can get around it without installing separate 
modules that define array/matrix objects?

rgds
 -- 
-------------------------------------------------------------------------
who     : george thomas
reduce (lambda c,d:chr(ord(d)-2)+c, 'ofcpqkekhhC"zwpkN"fgtgyqR/pqjv{R')
that is all
-------------------------------------------------------------------------




More information about the Python-list mailing list