2d lists

Shalabh Chaturvedi shalabh at cafepy.com
Sun May 30 14:19:33 EDT 2004


Shalabh Chaturvedi wrote:

> SunX wrote:
> 
>> What is the best way to assign a 2d lists?  Something like;
>>
>> for i in range(10):
>>     for j in range(10):
>>         aList[i][j] = i*j
>>
>> Thank you in advance.
> 
> 
> If you want a list of lists, you could do:
> 
> for i in range(10):
>    L = aList[i] = []
>    for j in range(10):
>        L[j] = i*j

This is quite wrong. Here is a corrected version (this time tested :)

aList = []
for i in range(10):
     L = []
     aList.append(L)
     for j in range(10):
         L.append(i*j)

--
Shalabh





More information about the Python-list mailing list