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() 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
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.
[(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), it will be
enumerate()as shown below.
numbers = [10,20,30,40,50] enum = enumerate(numbers,5) l = list(enum) print(l)
Notice that now index begins at 5.
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))
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))
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))
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))
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,t))
Notice that now there is a single loop variable which is a tuple.
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. 0