for loop: range() result has too many items

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Tue Oct 13 17:39:46 EDT 2009


On Tue, 13 Oct 2009 16:17:58 -0500, Peng Yu wrote:

> Hi,
> 
> The following code does not run because range() does not accept a big
> number. 

Incorrect.

>>> range(sys.maxint+2, sys.maxint+5)
[2147483649L, 2147483650L, 2147483651L]


> Is there a way to make the code work. I'm wondering if there is
> a way to write a for-loop in python similar to that of C style.
> 
> for(int i = 0; i < a_big_number; ++ i)

An exact translation of that would be a while loop:

i = 0
while i < a_big_number:
    i += 1


but that's not actually your problem. Your actual problem is that you're 
trying to generate a list containing 2147483647 (sys.maxint) items. Each 
item requires 8 bytes, so this will need a single contiguous block of at 
least 16 gigabytes.

On my system, when I try it, I get MemoryError. You get OverflowError.

Try instead using a lazy list:

>>> xrange(sys.maxint)
xrange(2147483647)



> def foo():
>   for i in range(sys.maxint):
>     if i % 100 == 0:
>       print i


Rather than iterating through every single integer between 0 and 
2147483647, you can skip the ones you don't care about:

for i in xrange(0, sys.maxint, 100):
    print i

This will run approximately 100 times faster than your code.



-- 
Steven



More information about the Python-list mailing list