Could someone explain this multidimensional list behaviour?

Aaron Sterling dhaaron at hotpop.com
Fri Nov 23 10:25:26 CET 2001


> >>> spam = []
> >>> for y in range(10):
> 	inner = []
> 	for x in range(10):
> 		inner.append(0)
> 	spam.append(inner)
> 	del inner
>

The second for loop should not be necessary. something like:

spam = []
for y in range(n):
    spam.append([0] * n)

should suffice

for m X n matrices just do:

spam = []
for y in range(m): 
    spam.append([0] * n)

Aaron Sterling, dhaaron at hotpop.com




More information about the Python-list mailing list