Most efficient way to "pre-grow" a list?

Luis Alberto Zarrabeitia Gomez kyrie at
Sat Nov 7 21:40:02 CET 2009

Quoting Andre Engels <andreengels at>:

> On Sat, Nov 7, 2009 at 8:25 PM, Luis Alberto Zarrabeitia Gomez
> <kyrie at> wrote:
> >
> > Ok, he has a dict.
> >
> > Now what? He needs a non-sparse array.
> Let d be your dict.
> Call the zeroeth place in your array d[0], the first d[1], the 10000th
> d[100000].

Following that reasoning, we could get rid of lists and arrays altogether.

Here's why that wouldn't work:

for x,y in zip(d,other):
    ... do something ...

Yes, we could also ignore zip and just use range/xrange to iterate for the

Lists and dictionaries have different semantics. One thing is to argue that you
shouldn't be thinking on pre-growing a list for performance reasons before being
sure that it is a bottleneck, and a very different one is to argue that because
one operation (__setitem__) is the same with both structures, we should not use
lists for what may need, depending on the problem, list semantics. 

¿Have you ever tried to read list/matrix that you know it is not sparse, but you
don't know the size, and it may not be in order? A "grow-able" array would just
be the right thing to use - currently I have to settle with either hacking
together my own grow-able array, or preloading the data into a dict, growing a
list with the [0]*size trick, and updating that list. Not hard, not worthy of a
PEP, but certainly not so easy to dismiss.

Luis Zarrabeitia
Facultad de Matemática y Computación, UH

Participe en Universidad 2010, del 8 al 12 de febrero de 2010
La Habana, Cuba

More information about the Python-list mailing list