How to make a For loop when you don't need an index in python?

if i need a for loop in python

for i in range(1,42):
    print "spam"

but don't use "i" for anything that pylint complains about an unused variable. How should I handle this? I know you can do this:

for dummy_index in range(1,42):
    print "spam"

but to me it seems rather strange, is there a better way?

I'm new to python, so forgive me if I miss something obvious.

+5
source share
5 answers

There is no β€œnatural” way to loop n times without a counter variable in Python, and you should not resort to ugly hacks to silence code analyzers.

In your case, I would suggest one of the following:

  • PyLint ( )
  • PyLint i, for.
  • , , _ ( , dummy)
+5
for _ in range(1,42):
    print "spam"
+10

pylint :

--dummy-variables-rgx=
          A regular expression matching names used for dummy variables (i.e.
          not used). [current: _|dummy]

, dummy, pylint :

for dummy in range(1, 42):
    print "spam"
+6

, :

>>> print "spam\n"*len(range(1,42))
+1

3

  • for.
  • But you can create a program that goes from index[1]to index[2]by simply adding if index[1]. return index[]+1.
  • Unfortunately, you need to create an additional program that is not as effective as the loop forand not effective in long programs.
0
source

All Articles