# [Numpy-discussion] when and where to use numpy arrays vs nested lists

Mark P. Miller mpmusu@cc.usu....
Thu Mar 1 10:03:48 CST 2007

```I've been using Numpy arrays for some work recently.  Just for fun, I
compared some "representative" code using Numpy arrays and an object
comprised of nested lists to represent my arrays.  To my surprise, the
array of nested lists outperformed Numpy in this particular application
(in my actual code by 10%, results below are more dramatic).

Can anyone shed some insight here?  The functions that I use in reality
are much more complicated than those listed below, but they are
nonetheless representative of the type of thing that I'm doing.

##imports
import numpy as NP
from numpy.random import randint

#numpy array code
array1 = NP.zeros((50,50), int)

def random1():
c = array1(randint(10), randint(10))

t=timeit.Timer("random1()", "from __main__ import random1")
>>> t.timeit(10000)
0.1085283185432786
>>> t.timeit(10000)
0.10784806448862128
>>> t.timeit(10000)
0.1095533091495895

#python 2d array based on nested lists
array2 = []
for aa in xrange(50):
array2.append([])
for bb in xrange(50):
array2[aa].append([])
array2[aa][bb] = 0

def random2():
c = array2[randint(50)][randint(50)]

>>> t=timeit.Timer("random2()", "from __main__ import random2")
>>> t.timeit(10000)
0.076737965300061717
>>> t.timeit(10000)
0.072883564810638291
>>> t.timeit(10000)
0.07668181291194287

Thanks,

-Mark
```