Define a 2d Array?

skip at pobox.com skip at pobox.com
Sun Oct 12 04:30:30 CEST 2008


    Jill> How do I define a 2d list?

Python doesn't truly have 2d lists in the way you might think of 2d arrays
in C or Fortran.  It has 1d lists which can contain any Python object,
including other lists.  If you wanted to create a 4x5 list you'd do
something like this:

    N = 4
    M = 5
    mylist = []
    for i in range(N):
        mylist.append([0.0] * M)

If you are looking to do numeric work with such multidimensional lists you
should consider the builtin array object or the numpy package:

    http://docs.python.org/dev/library/array.html#module-array
    http://numpy.scipy.org/

Skip



More information about the Python-list mailing list