Define a 2d Array?

John Machin sjmachin at lexicon.net
Sun Oct 12 17:38:52 EDT 2008


On Oct 12, 1:30 pm, s... at pobox.com wrote:
>     Jill> How do I define a 2d list?

>
> 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

The built-in array module does *NOT* support multidimensional arrays.

The referenced docs say (first two sentences) "This module defines an
object type which can compactly represent an array of basic values:
characters, integers, floating point numbers. Arrays are sequence
types and behave very much like lists, except that the type of objects
stored in them is constrained."

"behave very much like lists" is in no way to be construed as
supporting multiple dimensions, and that type constraint means that
you can't even have an array of arrays.

However you can have a list of arrays; this can be a memory-efficient
solution for some 2D applications. Note that arrays are not
recommended for CPU efficiency, as (in general) each time you access
an array element, a new Python object must be created. Escape clause:
CPython and single-byte arrays (type 'c' and some values of types 'b'
and 'B').

Cheers,
John



More information about the Python-list mailing list