By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,713 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,713 IT Pros & Developers. It's quick & easy.

Memory pre-allocation for large lists

P: n/a


Hello:

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?

Avi Kak
ka*@purdue.edu
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Avi Kak wrote:
Can the same be done in Python?


Not directly, but you can just fill it with something you're not going
to use:

arra = [None] * 999

--
__ Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
\__/ Of war men ask the outcome, not the cause.
-- Seneca
Jul 18 '05 #2

P: n/a


Thanks, Erik. That would work for me.

Avi
On Tue, 17 Feb 2004 22:00:09 -0800, Erik Max Francis <ma*@alcyone.com>
wrote:
Avi Kak wrote:
Can the same be done in Python?


Not directly, but you can just fill it with something you're not going
to use:

arra = [None] * 999


Jul 18 '05 #3

P: n/a
In article <if********************************@4ax.com>,
Avi Kak <ka*@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 (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"Argue for your limitations, and sure enough they're yours." --Richard Bach
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.