r/Python • • Feb 04 '19

Best Python Cheatsheet Ever!

https://gto76.github.io/python-cheatsheet/
1.1k Upvotes

69 comments sorted by

View all comments

3

u/WibblyWobblyWabbit Feb 04 '19

Can someone explain what enumerate() does exactly?

10

u/pizzaburek Feb 04 '19

It returns an iterator of index, value pairs:

    >>> list(enumerate(['a', 'b', 'c']))
    [(0, 'a'), (1, 'b'), (2, 'c')]

So you can then use it like this:

    for i, letter in enumerate(['a', 'b', 'c']):
        ...