how do you make a For loop when you don't need index in python? [duplicate]
Asked Answered
B

5

8

If I need a for loop in Python:

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

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

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

but doing this seems quite strange to me. Is there a better way?

I'm quite new to Python, so forgive me if I'm missing something obvious.

Barlow answered 12/4, 2012 at 10:51 Comment(2)
This is highly subjective. Either use a _ or dummy prefix, or decide on a different scheme and set PyLint's --dummy-variables-rgx option accordingly (e.g., unused_).Bernete
Actually, since "i" is used, and it is a necessary part of the syntax in the for statement, the only dummy thing around is pylint itself. I'd advise placing this as a bug report in the pylint project.Heptahedron
B
9

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

In your case I would suggest one of the following:

  • Just ignore the PyLint warning (or filter reported warnings for one-character variables)
  • Configure PyLint to ignore variables named i, that are usually only used in for loops anyway.
  • Mark unused variables using a prefix, probably using the default _ (it's less distracting than dummy)
Bernete answered 12/4, 2012 at 14:52 Comment(0)
M
18
for _ in range(1,42):
    print "spam"
Mas answered 12/4, 2012 at 10:52 Comment(5)
Thought it might recognise it since it is traditionally used when you have no use of the variable.Mas
+1: With default settings, PyLint does not complain about unused variables starting with "dummy" or "_". Using "_" to store unneeded values is common in Python (e.g. foo, _ = func_returning_tuple() Having said this, I would prefer _i over plain _.Bernete
Beware: using _ for this when also using the gettext library (or thinking you might use it in the future) will cause problems. Gettext uses _ as a i18n translating function (_("Translate this text")), but after the above loop _ would suddenly be 41 instead and the next invocation would raise an exception.Archpriest
This is what i was doing, but i was hoping someone would point me in some direction where i didn't have to declare the unused variable, rather than just ignoring it.Barlow
@Jacxel: There is no "natural" way to loop n times without a counter variable in Python, and you should not resort to ugly hacks around it. Either just ignore the PyLint warning, configure it to ignore unused variables named i, or use a prefix, probably the default _ (less distracting than dummy).Bernete
B
9

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

In your case I would suggest one of the following:

  • Just ignore the PyLint warning (or filter reported warnings for one-character variables)
  • Configure PyLint to ignore variables named i, that are usually only used in for loops anyway.
  • Mark unused variables using a prefix, probably using the default _ (it's less distracting than dummy)
Bernete answered 12/4, 2012 at 14:52 Comment(0)
F
8

According to pylint documentation:

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

In other words, if the name of the variable starts with an underscore, or with the letters dummy, pylint would not complain about the variable being unused:

for dummy in range(1, 42):
    print "spam"
Franco answered 12/4, 2012 at 10:57 Comment(1)
i gave that solution in my question, im asking if there is a way to do it without ever declaring the variableBarlow
S
1

Usually you can work around it, just like this in your case:

>>> print "spam\n"*len(range(1,42))
Steib answered 12/4, 2012 at 10:57 Comment(4)
But probably the OP wants to do something more complex, with this being a simple example...Swords
@PaulHiemstra what I'm saying is that probably even more complex things can be done without the unused variables. I can't imagine settings which cannot be solved without using dummy variablesSteib
while there probably is a workaround such as this, im trying to make as few changes to the code as possible. im working on getting an existing framework up to pep8 and want to avoid making big changes where unnecessary.Barlow
@luke14free, I understand, and I agreeSwords
F
0

3 Simple Reasons

  1. There is no way to loop through your program without using a counter variable in a for loop.
  2. But you can create a program that goes from index[1] to index[2] just by adding if index[1]is done. return index[]+1.
  3. Unfortunately, you need to create an extra program which is not as efficient as the for loop and is not efficient in long programs.
Firedog answered 30/3, 2016 at 23:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.