Getting the index of 'for' loops

PHOTO EMBED

Saved by @bravocoder #python

for idx, val in enumerate(ints):
    print(idx, val)
content_copyCOPY

Using an additional state variable, such as an index variable (which you would normally use in languages such as C or PHP), is considered non-pythonic. The better option is to use the built-in function enumerate(), available in both Python 2 and 3.

https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops