python loops

Fredrik Lundh fredrik at pythonware.com
Fri Sep 1 06:03:49 EDT 2006


Michael.Coll-Barth at VerizonWireless.com wrote:

> I thought the xrange was preferred?  for x in xrange(length):

preferred by premature optimization freaks, perhaps.  in practice, if the
range is reasonably small and you're going to loop over all the integers,
it doesn't really matter.

(the range form creates a list and N integers up front; the xrange form
creates an iterator object up front and N integers while you're looping.
what's faster depends on what Python version you're using, and some-
times also on the phase of the moon)

in Python 3.0, xrange() will disappear, and range() will return an iterator
instead.

</F> 






More information about the Python-list mailing list