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

List limits

P: n/a
How many items can be stored in a Python list? I have close to 70,000
items... is this within a lists limits?

Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
It is possible to store 70,000 items in a list (try "l =
range(70000)"), but the best way to check if you can store all the
items you need to store is to try it. After all if they are all very
large you might potentially run out of memory.

Chris

Jul 18 '05 #2

P: n/a
I'm referring to Python 2.2's C headers as I answer this question. I
believe some of may have changed by 2.4.

The number of elements in a "variable-sized object" (those with
Py_VAR_HEAD; I believe this includes lists, tuples, and strings) is
stored in a platform 'int'.

On most (desktop) systems, this means the limit of any sized object is
no more than 2**31-1, or about 2 billion.

On those same systems, a list object of length 128 million, give or
take, approximately 512 megabytes of memory will be allocated for the
list object itself (4 bytes for each pointer-to-element). If each
element of the list is distinct, those objects will each require
additional memory---The smallest useful Python object takes around 16
bytes, IIRC, which would bring the total memory required to around 2560
megabytes if I didn't screw up my arithmetic. This is close to (or
over, depending on the system) the maximum amount of physical RAM these
machines can accomodate, and the maximum amount of address space
available to a single program.

While performing list-resizing operations, there may be a temporary need
for two copies of the list object itself, bumping the memory used up to 3
gigs.

Finally, the speed of some operations (l.index(item), l.pop(0),
l.insert(0, item)) are related linearly to the size of the list, so your
program may slow down as the lists it manipulates grow. Others, such as
l[i], l.pop(), and l.append(item), are constant-time or amortized-constant-
time.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFBxw6pJd01MZaTXX0RAljUAJoD8/bR3hBgQOb3I40h+yKm5r9PqQCdEiBo
s1rX4NrllE9a+vWoQQpqFe4=
=F8Db
-----END PGP SIGNATURE-----

Jul 18 '05 #3

P: n/a
> How many items can be stored in a Python list? I have close to 70,000
items... is this within a lists limits?


Lists store pointers to objects. Unless you have a lot of duplicates, it is the
objects themselves that will consume most of your memory. The list itself will
likely be small in comparison.
Raymond Hettinger
Jul 18 '05 #4

P: n/a
Raymond Hettinger wrote:
How many items can be stored in a Python list? I have close to 70,000
items... is this within a lists limits?

Lists store pointers to objects. Unless you have a lot of duplicates, it is the
objects themselves that will consume most of your memory. The list itself will
likely be small in comparison.


Given the size of the counter, is it actually physically possible for a list to
run out of room before the application runs out memory?

Even list(None for x in xrange(sys.maxint)) wouldn't do the trick, since each of
those pointers to None is taking 4 bytes of memory, and Python's internal
structures are already chewing up some of the address space.

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #5

P: n/a
Nick Coghlan wrote:
Given the size of the counter, is it actually physically possible for a list to run out of room
before the application runs out memory?


depends on the system architecture, of course: consider an 64-bit computer
with 32-bit integers and 256 GB of memory...

</F>

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.