python loops

Fredrik Lundh fredrik at
Sun Sep 3 10:41:25 CEST 2006

Tim Roberts wrote:

> xrange used to be better.  As I understand it, that's no longer
 > the case.

for short ranges, range is faster in some Python versions, xrange is 
faster in some (including 2.4).  the difference is usually very small 
(the same number of objects are created in both cases).

for long ranges, especially when it's likely that you won't actually 
need all the values, xrange is better.

if you *need* a list, range is better.

in python 3.0, range will return an iterator, and xrange will disappear. 
  if you need a list in 3.0, you will have to do list(range(N)).


More information about the Python-list mailing list