The Python enumerate() function adds a counter to the iterable and returns the enumerate object. It iterates through a list while keeping track of the list item indices.

python enumerate() function

Python enumerate() Syntax

enumerate(iterable, start=0)

As you can see in the syntax above, enumerate() function takes two parameters.

  • iterable – Required. Iterable must be a sequence, an iterator, or some other object which supports iteration.
  • start – Optional. It’s the index at which enumeration shall start. If not included, enumeration starts from position 0.

How does Python enumerate() works?

Let’s explain with an example.

>>> months = ['Jan', 'Feb', 'Mar', 'Apr']
>>> list(enumerate(months))
[(0, 'Jan'), (1, 'Feb'), (2, 'Mar'), (3, 'Apr')]

Note: Enumerate objects can be converted into a list by using function list().

As you can see in above example, alongside the content, their corresponding indices are also printed out. And the enumerated object is converted into a list.

#Example 2 : Python enumerate() Function Example

>>> #when start is not mentioned
>>> for i in enumerate(('a', 'b', 'c')):
          print(i)

(0, 'a')
(1, 'b')
(2, 'c')

>>> #when start is mentioned
>>> for i in enumerate(('a', 'b', 'c'),start = 1):
          print(i)

(1, 'a')
(2, 'b')
(3, 'c')

As you can see in above example, in enumerate objects the index of the item is also printed alongside the item.

And also note that, when the second parameter start is not explicitly mentioned, indexing starts from 0 which is default else indexing starts from the point we mention with start (e.g in the second example, indexing started from 1 as we explicitly mentioned).