2d lists

beliavsky at aol.com beliavsky at aol.com
Sun May 30 19:44:03 EDT 2004


xiaohua_sun at yahoo.com (SunX) wrote in message news:<337e6cd5.0405291411.4376debc at posting.google.com>...
> 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.

(When I replied with subject "Re: 2d lists" Google groups complained.)

If you will use the 2d lists for numerical calculations, I would
recommend the Numeric or Numarray modules, which provide
multdimensional arrays that are much faster than lists of lists. A
restriction of Numeric or Numarray arrays is that all the elements
must be of the same type (as in Fortran).

With Numeric you could write

from Numeric import zeros
n = 10
alist = zeros([n,n])
for i in range(n):
    for j in range(n):
        alist[i,j] = i*j



More information about the Python-list mailing list