Python loop counter in a for loop
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Track title: CC L Beethoven - Piano Sonata No 8 in C
--
Chapters
00:00 Question
01:15 Accepted answer (Score 264)
01:42 Answer 2 (Score 7)
02:05 Answer 3 (Score 4)
02:24 Answer 4 (Score 3)
02:58 Thank you
--
Full question
https://stackoverflow.com/questions/1185...
Question links:
[PEP 212]: http://www.python.org/dev/peps/pep-0212/
[PEP 281]: http://www.python.org/dev/peps/pep-0281/
Accepted answer links:
[enumerate()]: http://docs.python.org/library/functions...
Answer 4 links:
[enumerate]: https://docs.python.org/2/library/functi...
[unpacking]: https://docs.python.org/2/tutorial/contr...
[itertools.count()]: https://docs.python.org/2/library/iterto...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#loops #forloop #python
#avk47
--
Track title: CC L Beethoven - Piano Sonata No 8 in C
--
Chapters
00:00 Question
01:15 Accepted answer (Score 264)
01:42 Answer 2 (Score 7)
02:05 Answer 3 (Score 4)
02:24 Answer 4 (Score 3)
02:58 Thank you
--
Full question
https://stackoverflow.com/questions/1185...
Question links:
[PEP 212]: http://www.python.org/dev/peps/pep-0212/
[PEP 281]: http://www.python.org/dev/peps/pep-0281/
Accepted answer links:
[enumerate()]: http://docs.python.org/library/functions...
Answer 4 links:
[enumerate]: https://docs.python.org/2/library/functi...
[unpacking]: https://docs.python.org/2/tutorial/contr...
[itertools.count()]: https://docs.python.org/2/library/iterto...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#loops #forloop #python
#avk47
ACCEPTED ANSWER
Score 269
Use enumerate() like so:
def draw_menu(options, selected_index):
for counter, option in enumerate(options):
if counter == selected_index:
print " [*] %s" % option
else:
print " [ ] %s" % option
options = ['Option 0', 'Option 1', 'Option 2', 'Option 3']
draw_menu(options, 2)
Note: You can optionally put parenthesis around counter, option, like (counter, option), if you want, but they're extraneous and not normally included.
ANSWER 2
Score 7
I'll sometimes do this:
def draw_menu(options, selected_index):
for i in range(len(options)):
if i == selected_index:
print " [*] %s" % options[i]
else:
print " [ ] %s" % options[i]
Though I tend to avoid this if it means I'll be saying options[i] more than a couple of times.
ANSWER 3
Score 4
You could also do:
for option in options:
if option == options[selected_index]:
#print
else:
#print
Although you'd run into issues if there are duplicate options.
ANSWER 4
Score 3
enumerate is what you are looking for.
You might also be interested in unpacking:
# The pattern
x, y, z = [1, 2, 3]
# also works in loops:
l = [(28, 'M'), (4, 'a'), (1990, 'r')]
for x, y in l:
print(x) # prints the numbers 28, 4, 1990
# and also
for index, (x, y) in enumerate(l):
print(x) # prints the numbers 28, 4, 1990
Also, there is itertools.count() so you could do something like
import itertools
for index, el in zip(itertools.count(), [28, 4, 1990]):
print(el) # prints the numbers 28, 4, 1990