Range() limit

Marc losnations at comcast.net
Sat Feb 22 02:46:36 EST 2003


Fortunately for me the max range I need for this application does not exceed
the max possible value for xrange(). I switched over and things are now
running smoothly. Thanks to the usual quick and helpful responses from those
here on the board.

And you know, I really had no idea that range() built a list like that. No
wonder I was killing my computer. I thought it was just an iterator. Eh,
live and learn...


"Magnus Lie Hetland" <mlh at furu.idi.ntnu.no> wrote in message
news:slrnb5dsht.81b.mlh at furu.idi.ntnu.no...
> In article <3E56D3FD.85281214 at alcyone.com>, Erik Max Francis wrote:
> >Marc wrote:
> >
> [snip]
> >
> >range _actually builds a list_.  If the number of elements in that list
> >is huge, you're going to be building a really big list.  If you're using
> >it solely for the purposes of iterating over, obviously this ain't the
> >brightest idea in the world.  Use xrange instead,
>
> That's good advice, of course, but won't really help. Once the limit
> exceets the integer range (and, thus, is a long) xrange will no longer
> work (for some reason I don't really understand -- why must xrange use
> ints?)
>
> >>> xrange(10**10)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> OverflowError: long int too large to convert to int
> >>>
>
> (This has actually burned me while running some benchmarks -- I ended
> up writing my own xrange-like generator that used longs.)
>
> > or simply count manually.
>
> That is (as far as I know) the only way, currently, to have counting
> loops with arbitrarily large upper limits.
>
> --
> Magnus Lie Hetland               "Nothing shocks me. I'm a scientist."
> http://hetland.org                                   -- Indiana Jones






More information about the Python-list mailing list