[Tutor] Long list error

Kent Johnson kent37 at tds.net
Mon Mar 9 03:06:37 CET 2009


On Sun, Mar 8, 2009 at 8:27 PM, William Stephens
<wstephens10 at suddenlink.net> wrote:
> Hello,
>
> I was working on a sieve of eratosthenes and ran into an error I don't
> understand.
>
>>>> size = 10 000 000 000
>>>> l = [0,1]*(size/2)
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> OverflowError: cannot fit 'long' into an index-sized integer
>
>
> Is there a type or something that I can do to prevent this error? Or am I on
> the wrong track for such large primes?

The maximum size of a list is given by Py_ssize_t in pyport.h in the
Python source and discussed in PEP 353:
http://www.python.org/dev/peps/pep-0353/

My understanding is that list size is limited to the maximum signed
integer supported by the underlying C compiler. On a 32-bit processor
this is 2**31 = 2,147,483,647 which is a bit smaller than your
requested 10,000,000,000.

But...think about what you are asking for. Each array element is 8
bytes (a pointer) so you are trying to create a 80,000,000,000 byte
array. That's about 74 gigabytes before you even try to put any values
in it.

So, yes, I think you are on the wrong track...

Kent


More information about the Tutor mailing list