using range() in for loops

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Apr 5 11:11:48 EDT 2006


On Wed, 05 Apr 2006 09:16:37 -0400, AndyL wrote:

> Paul Rubin wrote:
>> Normally you'd use range or xrange.  range builds a complete list in
>> memory so can be expensive if the number is large.  xrange just counts
>> up to that number.
> 
> so when range would be used instead of xrange. if xrange is more 
> efficient, why range was not reimplemented?

For historical reasons.

Don't worry, in Python3000, range() will be an iterator, and xrange() will
disappear. Until then, I use range() for small loops (by small I mean
anything up to a few tens of thousands), and xrange() only when I
absolutely have to optimize my code to save a piddling few tens of
kilobytes of memory.


-- 
Steven.




More information about the Python-list mailing list