maximum length of a list & tuple

Josiah Carlson jcarlson at uci.edu
Sun Apr 11 04:28:29 EDT 2004


>>Run the following and the last thing it prints out is your limit...
>>
>>    c = 1
>>    while c < 2**32:
>>        try:
>>            d = [1]*c
>>            print c
>>            c *= 2
>>        except:
>>            break
> 
> 
> Interesting experiment.  I get 64M on my 384MB machine, which suggests 4
> bytes per list entry.  Of course, now my swap file is packed full, and all
> my apps are running slowly while they page themselves back in...

Far more than 4 bytes per list entry.  4 bytes to store the integers 
themselves, but since integers are Python objects, and lists are arrays 
of pointers to list objects, there is quite a bit of other extra stuff 
attached.

I can't remember exactly how much extra stuff, but it can be found by 
looking through the sources.  You could check the amount of memory used 
before, and the memory used at peak, and divide it by your 64M to find 
out how much is used.  I just did, but I'll also leave it as an exercise 
to the reader.

  - Josiah



More information about the Python-list mailing list