python loops

Fredrik Lundh fredrik at
Fri Sep 1 12:03:49 CEST 2006

Michael.Coll-Barth at 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


More information about the Python-list mailing list