244

Is it possible to do following without the i?

for i in range(some_number):
    # do something

If you just want to do something N amount of times and don't need the iterator.

3
  • 25
    This is a good question! PyDev even flags the 'i' as a warning for 'unused variable'. The solution below removes this warning. Nov 16, 2009 at 10:26
  • 1
    @Ashwin You can use \@UnusedVariable to remove that warning. Note that I needed to escape the 'at' symbol to have this comment go through.
    – khatchad
    Oct 24, 2011 at 4:17
  • I head the same question you. It is annoying with pylint warnings. Of course you can disable the warnings by additional suppression like @Raffi Khatchadourian proposed. It would be nice to avoid pylint warnings and suppression comments.
    – tangoal
    Feb 14, 2020 at 15:30

16 Answers 16

143

Off the top of my head, no.

I think the best you could do is something like this:

def loop(f,n):
    for i in xrange(n): f()

loop(lambda: <insert expression here>, 5)

But I think you can just live with the extra i variable.

Here is the option to use the _ variable, which in reality, is just another variable.

for _ in range(n):
    do_something()

Note that _ is assigned the last result that returned in an interactive python session:

>>> 1+2
3
>>> _
3

For this reason, I would not use it in this manner. I am unaware of any idiom as mentioned by Ryan. It can mess up your interpreter.

>>> for _ in xrange(10): pass
...
>>> _
9
>>> 1+2
3
>>> _
9

And according to Python grammar, it is an acceptable variable name:

identifier ::= (letter|"_") (letter | digit | "_")*
12
  • 5
    "But I think you can just live with the extra "i"" Yeah it is just an academic point. May 4, 2009 at 5:16
  • 1
    @nemo, you can try doing for _ in range(n): if you don't want to use alphanumeric names.
    – Unknown
    May 4, 2009 at 5:19
  • 1
    @nemo Yes its just an acceptable variable name. In the interpreter, it is automatically assigned the last expression you made.
    – Unknown
    May 4, 2009 at 5:24
  • 4
    @kurczak There is a point. Using _ makes it clear that it should be ignored. Saying there's no point in doing this is like saying there's no point in commenting your code - because it would do exactly the same anyway. Jan 2, 2012 at 3:37
  • 2
    Another good reason for _ instead of i is that it appears that pylint (and perhaps other linters) will read _ as a discard and not give an unused variable warning. Apr 23, 2019 at 6:34
84

You may be looking for

for _ in itertools.repeat(None, times): ...

this is THE fastest way to iterate times times in Python.

11
  • 4
    I wasn't concerned with performance, I just was curious if there was a terser way to write the statement. While I have been using Python sporadically for about 2 years now I still feel there is a lot I am missing. Itertools is one of those things, thank you for the information. May 4, 2009 at 5:56
  • 7
    That's interesting, I wasn't aware of that. I just took a look at the itertools docs; but I wonder why is this faster than just using range or xrange?
    – si28719e
    May 4, 2009 at 6:02
  • 6
    @blackkettle: it's faster because it doesn't need to return the current iteration index, which is a measurable part of the cost of xrange (and Python 3's range, which gives an iterator, not a list). @nemo, range is as optimized as it can be, but needing to build and return a list is inevitably heavier work than an iterator (in Py3, range does return an iterator, like Py2's xrange; backwards compatibility doesn't permit such a change in Py2), especially one that doesn't need to return a varying value. May 7, 2009 at 14:17
  • 5
    @Cristian, yes, clearly preparing and returning a Python int every time, inc. gc work, does have a measurable cost -- using a counter internally is no matter. Sep 26, 2009 at 23:04
  • 5
    I understand now. The difference comes from the GC overhead, not from the "algorithm". By the way, I run a quick timeit benchmark and the speedup was ~1.42x. Sep 27, 2009 at 0:28
78

The general idiom for assigning to a value that isn't used is to name it _.

for _ in range(times):
    do_stuff()
21

What everyone suggesting you to use _ isn't saying is that _ is frequently used as a shortcut to one of the gettext functions, so if you want your software to be available in more than one language then you're best off avoiding using it for other purposes.

import gettext
gettext.bindtextdomain('myapplication', '/path/to/my/language/directory')
gettext.textdomain('myapplication')
_ = gettext.gettext
# ...
print _('This is a translatable string.')
1
  • 5
    To me this use of _ seems like a terrible idea, I wouldn't mind conflicting with it.
    – KeithWM
    Jan 12, 2019 at 21:57
10

Here's a random idea that utilizes (abuses?) the data model (Py3 link).

class Counter(object):
    def __init__(self, val):
        self.val = val

    def __nonzero__(self):
        self.val -= 1
        return self.val >= 0
    __bool__ = __nonzero__  # Alias to Py3 name to make code work unchanged on Py2 and Py3

x = Counter(5)
while x:
    # Do something
    pass

I wonder if there is something like this in the standard libraries?

3
  • 11
    I think having a method such as __nonzero__ with side-effects is a horrible idea. Apr 22, 2012 at 13:11
  • 3
    I would use __call__ instead. while x(): isn't that much harder to write.
    – Jasmijn
    Jul 9, 2012 at 14:16
  • 2
    There is also an argument for avoiding the name Counter; sure, it's not reserved or in the built-in scope, but collections.Counter is a thing, and making a class of the same name risks maintainer confusion (not that this isn't risking that already). May 3, 2018 at 13:17
7

You can use _11 (or any number or another invalid identifier) to prevent name-colision with gettext. Any time you use underscore + invalid identifier you get a dummy name that can be used in for loop.

1
  • Nice! PyDev agrees with you: this gets rid of the "Unused variable" yellow warning. May 26, 2015 at 6:58
2

May be answer would depend on what problem you have with using iterator? may be use

i = 100
while i:
    print i
    i-=1

or

def loop(N, doSomething):
    if not N:
        return
    print doSomething(N)
    loop(N-1, doSomething)

loop(100, lambda a:a)

but frankly i see no point in using such approaches

1
  • 1
    Note: Python (definitely not the CPython reference interpreter at least, probably not most of the others) does not optimize out tail recursion, so N will be limited to something in the neighborhood of the value of sys.getrecursionlimit() (which defaults to somewhere in the low four digit range on CPython); using sys.setrecursionlimit would raise the limit, but eventually you'd hit the C stack limit and the interpreter would die with a stack overflow (not just raising a nice RuntimeError/RecursionError). Jun 20, 2018 at 3:10
0

I generally agree with solutions given above. Namely with:

  1. Using underscore in for-loop (2 and more lines)
  2. Defining a normal while counter (3 and more lines)
  3. Declaring a custom class with __nonzero__ implementation (many more lines)

If one is to define an object as in #3 I would recommend implementing protocol for with keyword or apply contextlib.

Further I propose yet another solution. It is a 3 liner and is not of supreme elegance, but it uses itertools package and thus might be of an interest.

from itertools import (chain, repeat)

times = chain(repeat(True, 2), repeat(False))
while next(times):
    print 'do stuff!'

In these example 2 is the number of times to iterate the loop. chain is wrapping two repeat iterators, the first being limited but the second is infinite. Remember that these are true iterator objects, hence they do not require infinite memory. Obviously this is much slower then solution #1. Unless written as a part of a function it might require a clean up for times variable.

1
  • 2
    chain is unnecessary, times = repeat(True, 2); while next(times, False): does the same thing.
    – AChampion
    Oct 18, 2015 at 5:39
0

We have had some fun with the following, interesting to share so:

class RepeatFunction:
    def __init__(self,n=1): self.n = n
    def __call__(self,Func):
        for i in xrange(self.n):
            Func()
        return Func


#----usage
k = 0

@RepeatFunction(7)                       #decorator for repeating function
def Job():
    global k
    print k
    k += 1

print '---------'
Job()

Results:

0
1
2
3
4
5
6
---------
7
0

If do_something is a simple function or can be wrapped in one, a simple map() can do_something range(some_number) times:

# Py2 version - map is eager, so it can be used alone
map(do_something, xrange(some_number))

# Py3 version - map is lazy, so it must be consumed to do the work at all;
# wrapping in list() would be equivalent to Py2, but if you don't use the return
# value, it's wastefully creating a temporary, possibly huge, list of junk.
# collections.deque with maxlen 0 can efficiently run a generator to exhaustion without
# storing any of the results; the itertools consume recipe uses it for that purpose.
from collections import deque

deque(map(do_something, range(some_number)), 0)

If you want to pass arguments to do_something, you may also find the itertools repeatfunc recipe reads well:

To pass the same arguments:

from collections import deque
from itertools import repeat, starmap

args = (..., my args here, ...)

# Same as Py3 map above, you must consume starmap (it's a lazy generator, even on Py2)
deque(starmap(do_something, repeat(args, some_number)), 0)

To pass different arguments:

argses = [(1, 2), (3, 4), ...]

deque(starmap(do_something, argses), 0)
0

Instead of an unneeded counter, now you have an unneeded list. Best solution is to use a variable that starts with "_", that tells syntax checkers that you are aware you are not using the variable.

x = range(5)
while x:
  x.pop()
  print "Work!"
0

We can use the while & yield, we can create our own loop function like this. Here you can refer to the official documentation.

def my_loop(start,n,step = 1):
    while start < n:
        yield start
        start += step

for x in my_loop(0,15):
    print(x)
0

In an answer to a related question, @japs timed four approaches at doing do_something() N times: a throwaway variable (standard), an underscore, itertools.repeat() (loopiter), and map(do_something, itertools.repeat()) (loopiter2). This yielded this comparison with clear differences:

standard:  0.8398549720004667
underscore:  0.8413165839992871
loopiter:  0.7110594899968419
loopiter2:  0.5891903560004721

I decided to loop inside do_something() so that it actually takes some time. This yielded this comparison:

standard: 8.756060799933039
underscore: 8.730245499988087
loopiter: 8.643029399914667
loopiter2: 8.489477000082843

My conclusion is that the cost of looping pales in comparison with the cost of doing something. The moral of this story is its better to focus on optimizing what you do instead of how you loop.

0

The simplest way I've know is del:

for i in range(some_number):
    del i
    # do something

del simply deletes the variable from memory. No warnings, etc.

-2

If you really want to avoid putting something with a name (either an iteration variable as in the OP, or unwanted list or unwanted generator returning true the wanted amount of time) you could do it if you really wanted:

for type('', (), {}).x in range(somenumber):
    dosomething()

The trick that's used is to create an anonymous class type('', (), {}) which results in a class with empty name, but NB that it is not inserted in the local or global namespace (even if a nonempty name was supplied). Then you use a member of that class as iteration variable which is unreachable since the class it's a member of is unreachable.

2
  • 1
    Obviously this is intentionally pathological, so criticizing it is beside the point, but I will note an additional pitfall here. On CPython, the reference interpreter, class definitions are naturally cyclic (creating a class unavoidably creates a reference cycle that prevents deterministic cleanup of the class based on reference counting). That means you're waiting on cyclic GC to kick in and clean up the class. It will usually be collected as part of the younger generation, which by default is collected frequently, but even so, each loop means ~1.5 KB of garbage w/non-deterministic lifetime. May 3, 2018 at 13:46
  • Basically, to avoid a named variable that would be (typically) deterministically cleaned up on each loop (when it's rebound, and the old value cleaned up), you're making a huge unnamed variable that is cleaned non-deterministically, and could easily last longer. May 3, 2018 at 13:47
-8

What about:

while range(some_number):
    #do something
2
  • 4
    That's an infinite loop as the condition range(some_number) is always true!
    – deadly
    Oct 11, 2012 at 10:44
  • 1
    @deadly: Well, if some_number is less than or equal to 0, it's not infinite, it just never runs. :-) And it's rather inefficient for an infinite loop (especially on Py2), since it creates a fresh list (Py2) or range object (Py3) for each test (it's not a constant from the interpreter's point of view, it has to load range and some_number every loop, call range, then test the result). May 3, 2018 at 14:07

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.