[Cython] Cython syntax to pre-allocate lists for performance

Stefan Behnel stefan_ml at behnel.de
Thu Mar 7 12:21:39 CET 2013

Yury V. Zaytsev, 07.03.2013 12:16:
> Is there any syntax that I can use to do something like this in Cython:
>     py_object_ = PyList_New(123); ?

Note that Python has an algorithm for shrinking a list on appending, so
this might not be sufficient for your use case.

> If not, do you think that this can be added in one way or another?
> Unfortunately, I can't think of a non-disruptive way of doing it. For
> instance, if this
>     [None] * N
> is given a completely new meaning, like make an empty list (of NULLs),
> instead of making a real list of Nones, it will certainly break Python
> code. Besides, it would probably be still faster than no pre-allocation,
> but slower than an empty list with pre-allocation...
> Maybe
>     [NULL] * N ?

What do you need it for?

Won't list comprehensions work for you? They could potentially be adapted
to presize the list.

And why won't [None]*N help you out? It should be pretty cheap.


More information about the cython-devel mailing list