newbie question-multidimensional arrays

Dan Bishop danb_83 at
Wed Jun 16 01:15:03 CEST 2004

"Miki Tebeka" <miki.tebeka at> wrote in message news:<mailman.963.1087276893.6949.python-list at>...
> Hello Doug,
> > How do you define and access multidimensional arrays in Python?   I am new
> > to python and come from a C and FORTRAN background and I am not sure how to
> > define an array without giving the contents of the array.  Any help will be
> > appreciated.   I would like to be able to read and manipulate
> > multidimensional arrays using a nested loop.  I cannot find anything in the
> > documentation to help me.
> Python lists are arrays that can hold anything including other lists.
> This makes them a multidimensional arrays as well.
> For example:
> def gen_2darray(m, n, initial=0):
>     '''Generate two dimensional array
>     We can't use [[] * n] * m since the internal arrays will point to
>     the same array.
>     '''
>     arr = [None] * m
>     for i in range(m):
>         arr[i] = [initial] * n
>     return arr

In Python 2.0(?) and later, this can be written as

      return [[initial] * m for i in xrange(n)]

More information about the Python-list mailing list