# 2D array

Wed Dec 8 19:15:42 CET 2004

```On Tue, 2004-12-07 at 23:02, Steven Bethard wrote:
> LutherRevisited wrote:
> > I'm wanting to do something with a list that is basically a 2 dimensional
> > array.  I'm not so good with lists so can someone give me an example of how I
> > might implement this in Python?  thanks.
>
> If you're planning to do anything serious with a 2D array, you should
> probably look at numarray:
>      http://www.stsci.edu/resources/software_hardware/numarray
>
>  >>> import numarray as na
>  >>> arr = na.array(range(10), shape=(5, 2))
>  >>> arr
> array([[0, 1],
>         [2, 3],
>         [4, 5],
>         [6, 7],
>         [8, 9]])
>  >>> arr[0,1]
> 1
>  >>> arr[4,0]
> 8
>
> If you're not doing any heavy computation, you can probably do this with
> nested lists:
>
>  >>> arr = [[0, 1],
> ...        [2, 3],
> ...        [4, 5],
> ...        [6, 7],
> ...        [8, 9]]
>  >>> arr
> 1
>  >>> arr
> 8
>
> Steve

If your data is sparse you might want to consider using a dictionary
where the key is a tuple representing the coordinates.

a = {}
a[(0,0)] = 0
a[(0,1)] = 1
a[(1,0)] = 2
a[(1,1)] = 3
a[(2,0)] = 4
a[(2,1)] = 5
a[(3,0)] = 6
a[(3,1)] = 7
a[(4,0)] = 8
a[(4,1)] = 9

>>> a.get( (3,0), None )
6
>>> print a.get( (5,0), None )
None