efficiency of range() and xrange() in for loops

Fredrik Lundh fredrik at pythonware.com
Wed Apr 5 16:24:24 EDT 2006


Steve R. Hastings wrote:

> If Python actually allocates the list, then clearly we should all use
> "for i in xrange".

clearly we should all?  speak for yourself.  the difference isn't very
large, xrange is actually slower in some python versions, and you'll
need the integer objects sooner or later anyway...

> If Python doesn't currently optimize this case, is there any chance this
> optimization could be added?

in Python 2.X, range is defined to return a list.  if you start returning
something else, you'll break stuff.

</F>






More information about the Python-list mailing list