9

i would like to ask what is the best way to make simple iteration. suppose i want to repeat certain task 1000 times, which one of the following is the best? or is there a better way?

for i in range(1000):
    do something with no reference to i

i = 0
while i < 1000:
    do something with no reference to i
    i += 1

thanks very much

nos
  • 19,875
  • 27
  • 98
  • 134
  • I prefer the first one ... I believe it's just a matter of personal taste – William Dec 14 '10 at 05:44
  • The first one is more idiomatic. – Adam Vandenberg Dec 14 '10 at 05:45
  • "which one of the following is the best"? What do **you** mean by "best"? Please define "best". Without a definition for "best" either of these could be better. Indeed, there are yet more ways to do this, which -- depending on your definition of "best" -- could be best. Please define "best". – S.Lott Dec 14 '10 at 12:28
  • Oh, and did I mention, please define best. – Jeff Aug 04 '16 at 20:25

4 Answers4

8

The first is considered idiomatic. In Python 2.x, use xrange instead of range.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • +1 for `xrange`, which has almost the exact same performance characteristics (at least in the CPython implementation). – Travis Gockel Dec 14 '10 at 05:48
  • is xrange() always better than range() ? when should one use range() instead of xrange()? – nos Dec 14 '10 at 05:49
  • http://stackoverflow.com/questions/135041/should-you-always-favor-xrange-over-range – user225312 Dec 14 '10 at 05:50
  • In 2.x, `range()` creates the list of numbers ahead of time, iterates through it, and then throws it away. `xrange()` (`range()` in 3.x) creates a special "generator" object that returns the numbers on demand, which is more efficient. In 3.x, if you actually want the list, you must ask for it: e.g. `list(xrange(10))` will use the list constructor to make a list out of the values returned from the generator. – Karl Knechtel Dec 14 '10 at 05:51
  • See also my answer with figures – Chris Morgan Dec 14 '10 at 05:55
6

The for loop is more concise and more readable. while loops are rarely used in Python (with the exception of while True).

A bit of idiomatic Python: if you're trying to do something a set number of times with a range (with no need to use the counter), it's good practice to name the counter _. Example:

for _ in range(1000):
    # do something 1000 times
Rafe Kettler
  • 75,757
  • 21
  • 156
  • 151
  • Personally I would never use `_` for a loop but always `i`, `j`, `k`. In the interactive console, `_` is the previous result, and in programs `_` will often be used for i18n (much less than in other languages as `_` *is* seen more as a throwaway variable in Python). – Chris Morgan Dec 14 '10 at 05:55
  • @Chris Agreed for the interactive interpreter. As for localization, I've never seen that in any language. – Rafe Kettler Dec 14 '10 at 06:03
  • haven't seen `_('string')`? It's a common usage pattern with gettext. – Chris Morgan Dec 14 '10 at 07:37
  • @Chris nope, or maybe I never noticed it – Rafe Kettler Dec 14 '10 at 15:45
5

In Python 2, use

for i in xrange(1000):
    pass

In Python 3, use

for i in range(1000):
    pass

Performance figures for Python 2.6:

$ python -s -m timeit '' 'i = 0
> while i < 1000:
>     i += 1'
10000 loops, best of 3: 71.1 usec per loop

$ python -s -m timeit '' 'for i in range(1000): pass'
10000 loops, best of 3: 28.8 usec per loop

$ python -s -m timeit '' 'for i in xrange(1000): pass'
10000 loops, best of 3: 21.9 usec per loop

xrange is preferable to range in this case because it produces a generator rather than the whole list [0, 1, 2, ..., 998, 999]. It'll use less memory, too. If you needed the actual list to work with all at once, that's when you use range. Normally you want xrange: that's why in Python 3, xrange(...) becomes range(...) and range(...) becomes list(range(...)).

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
  • On my system, `range` continues to perform more or less linearly up to about 130,000 elements or so, and then takes a big jump in time/loop/iteration and continues to get worse. `xrange` is unstoppable up to at least a hundred million elements, and I would only expect it to start showing any kind of slowdown at all somewhere around the 2-4 billion mark. ;) – Karl Knechtel Dec 14 '10 at 06:13
0

first. because the integer is done in the internal layer rather than interpretor. Also one less global variable.

leon
  • 4,931
  • 7
  • 39
  • 37