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

Raymond Hettinger python at rcn.com
Fri Nov 6 12:39:09 EST 2009


[kj]
> In fact, one would need to pre-grow the list sufficiently to be
> able to make an assignment like this one.  I.e. one needs the
> equivalent of the second Perl snippet above.
>
> The best I can come up with is this:
>
> arr = [None] * 1000000
>
> Is this the most efficient way to achieve this result?

Yes.


Raymond




More information about the Python-list mailing list