using range() in for loops

Sion Arrowsmith siona at chiark.greenend.org.uk
Wed Apr 5 10:38:57 EDT 2006


AndyL  <ask at me> 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?

If you actually want the list for some reason:

$ python2.4 -mtimeit 'list(xrange(100))'
100000 loops, best of 3: 4.54 usec per loop
$ python2.4 -mtimeit 'range(100)'
100000 loops, best of 3: 2.61 usec per loop

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list