Python enumerate() function

Python enumerate() function is used to add an index to the elements of an iterable while looping over it. Here, iterable means a structure that can be iterated such as a list, tuple or a string

Many times while looping over a list(or any other structure), you need the index or position of current element which you do not get directly using a while or for-in loop.

enumerate() function is used for this purpose only, it adds an index to each element of the iterable and this article will explain how to use enumerate() over a list, tuple and string.

enumerate() syntax
enumerate() function accepts two arguments:
1. iterable: This is the structure over which you are iterating or looping.
2. index: Custom index of elements. This argument is optional, if omitted, defaults to zero since elements in any iterable start from 0.

enumerate() returns an object of enumerate class
enumerate() example
Below is an example of enumerate() function with a python list.

# list of integers
numbers = [10,20,30,40,50]
# print list
print(numbers)
# enumerate function
enum = enumerate(numbers)
# print the type of enumerate
print(type(enum))

# create a new list
enum_list = list(enum)
print(enum_list)

Above example defines a list of integers and passes it to enumerate(). The return value from enumerate() is used to create another list by passing it to list constructor.
Original list, type of value returned by enumerate() and the list created with it are printed and below is the output.

[10, 20, 30, 40, 50] <class ‘enumerate’>
[(0, 10), (1, 20), (2, 30), (3, 40), (4, 50)]

You can see that list created using enumerate() has an index added to each element.
If you carefully observe, each element of the list created with enumerate() is actually a python tuple and if you print the type of any element as type(enum_list[0]), it will be

<class ‘tuple’>

Default index of list elements starts at 0 but it can be customized by providing second argument to enumerate() as shown below.

numbers = [10,20,30,40,50]
enum = enumerate(numbers,5)
l = list(enum)
print(l)

which prints

[(5, 10), (6, 20), (7, 30), (8, 40), (9, 50)]

Notice that now index begins at 5.
enumerate list
Python enumerate() function can be used to iterate list with index using a for loop as shown below.

# list of integers
numbers = [10,20,30,40,50]
# enumerate function
enum = enumerate(numbers)
for index, number in enumerate(numbers):
   print('Index=%d, Number=%d' %(index,number))

which prints

Index=0, Number=10
Index=1, Number=20
Index=2, Number=30
Index=3, Number=40
Index=4, Number=50

enumerate tuple
Python enumerate() function can be used to iterate a tuple using a for loop similar to list as given ahead.

# list of integers
parts = ('mouse', 'printer', 'keyboard')

for index, part in enumerate(parts):
   print('Index=%d, Part=%s' %(index,part))

Output is

Index=0, Part=mouse
Index=1, Part=printer
Index=2, Part=keyboard

enumerate string
With each character, if you want its index as well, then enumerate() function can do it for you as shown below.

str = 'codippa'

for index, char in enumerate(str):
   print('Index=%d, Part=%s' %(index,char))

This prints

Index=0, Part=c
Index=1, Part=o
Index=2, Part=d
Index=3, Part=i
Index=4, Part=p
Index=5, Part=p
Index=6, Part=a

enumerate a list of tuples
A list of tuples means each element of a list is a tuple.
A list of tuples can be easily iterated using enumerate() function in a much cleaner syntax.

list_of_tuples = [('dozen',12),('score',20),('gross',144)]
for index, (qty, number) in enumerate(list_of_tuples):
   print('Index=%d, Quantity=%s, Number=%d' %(index,qty,number))

Here, enumerate() will unpack the list of tuples and so the first element will be index of each list item and second element will be tuple from list item.
This will print

Index=0, Quantity=dozen, Number=12
Index=1, Quantity=score, Number=20
Index=2, Quantity=gross, Number=144

enumerate with index
As stated earlier in this article, enumerate() converts each item of the list to a tuple whose first element is the index and second is the list element.
Thus, you can also access index and element directly as we do it in a tuple using square brackets. Example is given below.

# list of integers
numbers = [10,20,30,40,50]
# enumerate function
enum = enumerate(numbers)
for t in enumerate(numbers):
   print('Index=%d, Number=%d' %(t[0],t[1]))

Notice that now there is a single loop variable which is a tuple.

In this article you learnt how enumerate() with a list, tuple or string adds an index before each of its element and when you iterate over the sequence, you can access both index and the element itself.