how do you make a For loop when you don't need index in python?
-
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 at python so forgive me if I'm missing something obvious.
-
Answer:
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)
Jacxel at Stack Overflow Visit the source
Other answers
for _ in range(1,42): print "spam"
jamylak
According to http://linux.die.net/man/1/pylint: --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"
aix
Usually you can work around it, just like this in your case: >>> print "spam\n"*len(range(1,42))
luke14free
Related Q & A:
- How do I make a python web program that is on a ubuntu server allow access to the server?Best solution by Yahoo! Answers
- How do i make a signature for when i send an email?Best solution by Yahoo! Answers
- How do I make a henna tattoo darker when the paste is already off?Best solution by leezees.com
- What is it called when you don't have a knuckle?Best solution by Yahoo! Answers
- Things to do in the summer when you don't have a pool?Best solution by viralnova.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.