0 works

Getting the index of 'for' loops


dashboard+ Project

Posted by @bravocoder #python

Code snippet:

for idx, val in enumerate(ints):
    print(idx, val)
content_copyCopy to Clipboard

More details:

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.

Code snippet source:

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

>> Browse more code snippets

more_vert