11

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

4
  • I prefer the first one ... I believe it's just a matter of personal taste Commented Dec 14, 2010 at 5:44
  • The first one is more idiomatic. Commented Dec 14, 2010 at 5: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". Commented Dec 14, 2010 at 12:28
  • Oh, and did I mention, please define best. Commented Aug 4, 2016 at 20:25

4 Answers 4

8

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

Sign up to request clarification or add additional context in comments.

5 Comments

+1 for xrange, which has almost the exact same performance characteristics (at least in the CPython implementation).
is xrange() always better than range() ? when should one use range() instead of xrange()?
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.
See also my answer with figures
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

4 Comments

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 Agreed for the interactive interpreter. As for localization, I've never seen that in any language.
haven't seen _('string')? It's a common usage pattern with gettext.
@Chris nope, or maybe I never noticed it
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(...)).

1 Comment

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. ;)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.