2d lists

Shalabh Chaturvedi shalabh at cafepy.com
Sun May 30 11:38:57 EDT 2004


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

A completely different option is to use a dict for 2d data:

d = {}

for i in range(10):
    for j in range(10):
      d[(i,j)] = i*j

Notice the index is a tuple.

--
Shalabh





More information about the Python-list mailing list