enumerate

Resources

  • Official documentation for the enumerate function.

enumerate

In Python the natural way of writing for loops is to iterate through iterable objects or iterators.

Sometimes it is useful to know the index of the item that we’re processing in the loop. We could get it by doing:

To solve this issue in an easy way Python has the enumerate function.

Internally, enumerate works by creating an iterable object that keeps yielding tuples with the index and the item in the original iterable.

An each tuple is then unpacked in the for loop by using the standard tuple unpacking mechanism.