Last Updated: February 25, 2016
·
4.435K
· 3demax

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