62

In Python you have two fine ways to repeat some action more than once. One of them is while loop and the other - for loop. So let's have a look on two simple pieces of code:

for i in range(n):
    do_sth()

And the other:

i = 0
while i < n:
    do_sth()
    i += 1

My question is which of them is better. Of course, the first one, which is very common in documentation examples and various pieces of code you could find around the Internet, is much more elegant and shorter, but on the other hand it creates a completely useless list of integers just to loop over them. Isn't it a waste of memory, especially as far as big numbers of iterations are concerned?

So what do you think, which way is better?

Amber
  • 507,862
  • 82
  • 626
  • 550
Sventimir
  • 1,996
  • 3
  • 14
  • 25

4 Answers4

71

but on the other hand it creates a completely useless list of integers just to loop over them. Isn't it a waste of memory, especially as far as big numbers of iterations are concerned?

That is what xrange(n) is for. It avoids creating a list of numbers, and instead just provides an iterator object.

In Python 3, xrange() was renamed to range() - if you want a list, you have to specifically request it via list(range(n)).

Amber
  • 507,862
  • 82
  • 626
  • 550
21

This is lighter weight than xrange (and the while loop) since it doesn't even need to create the int objects. It also works equally well in Python2 and Python3

from itertools import repeat
for i in repeat(None, 10):
    do_sth()
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
  • It requires importing itertools on the other hand, which is a bit inconvenient. Amber's answer looks better. – Sventimir Jul 15 '13 at 11:05
  • 11
    It's inconvenient for code golf. Not sure why people think import is some sort of problem. Besides real programs have a bunch of imports and itertools has lots of stuff you should be using anyway. – John La Rooy Jul 15 '13 at 11:09
  • 1
    Well, I assume that when you could do something with the core language equally good as with a module, then why import? Itertools is extremely useful for certain purposes, true, but I can imagine a lot of Python scripts working nicely without it. That's why it's a module and not core Python. :) – Sventimir Jul 15 '13 at 13:06
  • 1
    `for i in xrange(n):` is also more idiomatic, which is honestly far more important than an `import` either way. – Amber Jul 15 '13 at 15:13
  • You can also repeat the method you are intending to call: `[fn() for fn in repeat(self.callable_thing, 10)]` ... should save a lookup on each iteration. – F1Rumors May 10 '16 at 03:36
  • 2
    @F1Rumors, Using a list comprehension purely for side effects is generally discouraged. – John La Rooy May 10 '16 at 05:50
  • Fair point @JohnLaRooy, I was capturing the results, which is not part of the original question. In that form, `for fn in repeat(self.callable_thing, 10): fn()` is the right thing to do... – F1Rumors May 10 '16 at 14:21
9

python3 & python2

just use range():

for _ in range(n):
    # do something n times exactly
wahaha
  • 311
  • 4
  • 3
2

The fundamental difference in most programming languages is that unless the unexpected happens a for loop will always repeat n times or until a break statement, (which may be conditional), is met then finish with a while loop it may repeat 0 times, 1, more or even forever, depending on a given condition which must be true at the start of each loop for it to execute and always false on exiting the loop, (for completeness a do ... while loop, (or repeat until), for languages that have it, always executes at least once and does not guarantee the condition on the first execution).

It is worth noting that in Python a for or while statement can have break, continue and else statements where:

  • break - terminates the loop
  • continue - moves on to the next time around the loop without executing following code this time around
  • else - is executed if the loop completed without any break statements being executed.

N.B. In the now unsupported Python 2 range produced a list of integers but you could use xrange to use an iterator. In Python 3 range returns an iterator.

So the answer to your question is 'it all depends on what you are trying to do'!

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
  • It is absolutely not true that a `for` loop will **always** repeat `n` times. It is very common to have `break` statements inside `for` loops to make them repeat less than `n` times. – Andrés Mejía Sep 05 '20 at 23:57
  • 1
    @AndrésMejía - my original answer did state "unless the unexpected" - my edit should clarify with details on break, continue and else. – Steve Barnes Sep 06 '20 at 05:33