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

sturlamolden sturlamolden at
Sun Nov 8 02:03:37 CET 2009

On 6 Nov, 13:12, kj <no.em... at> wrote:

> The best I can come up with is this:
> arr = [None] * 1000000
> Is this the most efficient way to achieve this result?

Yes, but why would you want to? Appending to a Python list has
amortized O(1) complexity. I am not sure about Perl, but in MATLAB
arrays are preallocated because resize has complexity O(n), instead of
amortized O(1). You don't need to worry about that in Python. Python
lists are resized with empty slots at the end, in proportion to the
size of the list. On average, this has the same complexity as pre-

More information about the Python-list mailing list