for loop: range() result has too many items

Stephen Hansen apt.shansen at gmail.com
Tue Oct 13 23:53:47 CEST 2009


On Tue, Oct 13, 2009 at 2:17 PM, Peng Yu <pengyu.ut at gmail.com> wrote:

> Hi,
>
> The following code does not run because range() does not accept a big
> number. 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)
>
>
Don't use range(); range creates allocates an entire list-- thus its quite
expensive for big numbers.

Use xrange instead, which generates one number at a time as its consumed.

HTH,

--S
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091013/33c2d049/attachment.html>


More information about the Python-list mailing list