Memory pre-allocation for large lists

Aahz aahz at pythoncraft.com
Wed Feb 18 02:17:41 EST 2004


In article <ift530d51l8td0uoohm5t9ad96pq5n4e3i at 4ax.com>,
Avi Kak  <kak at purdue.edu> wrote:
>
>Is it possible in Python to define an empty list of a predetermined
>size?
>
>To illustrate my question further, I can do the following in Perl
>
>   my @arr = ();
>   $#arr = 999;
>
>that wold cause @arr to become an empty array of size 1000.  This
>array could be used to store 1000 elements without any further memory
>allocation for the array itself.
>
>Can the same be done in Python?

Can, yes.  But why?  Python uses an allocation mechanism for lists that
results in constant amortized time; there's really no reason to
pre-allocate.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Argue for your limitations, and sure enough they're yours."  --Richard Bach



More information about the Python-list mailing list