473,385 Members | 1,325 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Memory pre-allocation for large lists



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
3 4163
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ian | last post by:
Hi all, I have a problem. I have an application which needs to work with a lot of data, but not all at the same time. It is arranged as a set of objects, each with lots of data that is created...
1
by: Harsha | last post by:
I have been working on VB, ASP for quite a long time. Very Recently ( From past 1 month) I am managing a VC++ project. I am trying to use the code which i downloaded from the internet to find the...
10
by: Dan Elliott | last post by:
I am working on some tricky code and need some help from the experts. I have several large data structures (uBLAS matrices) that must be written to a pre-allocated (by another program) chunk of...
14
by: Greg Copeland | last post by:
I am running python on VxWorks. In the course of operation, a vxworks tasks writes to a reserved area of memory. I need access to this chunk of memory from within python. Initially I thought I...
81
by: Peter Olcott | last post by:
It looks like System::Collections::Generic.List throws and OUT_OF_MEMORY exception whenever memory allocated exceeds 256 MB. I have 1024 MB on my system so I am not even out of physical RAM, much...
17
by: christophe.chazeau | last post by:
Hi, I have a problem with a really simple chunk of code which should work but does obviously does not. This chunk of code is just a POC aimed at finding a bug in a larger project in which the...
3
by: Mad Hatter | last post by:
Hi folks I have a script which reads the mysql database,does a lot of string manipulation then writes its output to a file. The manipulation is all done in a separate function which is called...
27
by: George2 | last post by:
Hello everyone, Should I delete memory pointed by pointer a if there is bad_alloc when allocating memory in memory pointed by pointer b? I am not sure whether there will be memory leak if I do...
2
by: irm2216 | last post by:
I'm trying to make a memory game where you try to link up the two corresponding colored circles, but if they are the wrong colors, they go back to being a blank canvas. The graphics methods do not...
11
by: dhtml | last post by:
(originally mis-posted on m.p.s.jscript...) I've just closed all windows in Firefox and its using 244MB of memory. I have no idea why. I had GMail open, a page from unicode, the CLJ FAQ. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.