yprpyq
Last Updated: February 25, 2016
·
1.586K
· 3demax
B91775f48b21e9eb536ee9b955675ed4

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

Say Thanks
Respond