Iteration counter in for loops in python
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)
The trick is using enumerate. Pretty self-explanatory.
from http://stackoverflow.com/questions/1185545/python-loop-counter-in-a-for-loop
Written by Max Golubev
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#