Often, when dealing with iterators, we also need to keep a count of iterations. Python eases the programmers’ task by providing a built-in function enumerate() for this task. The enumerate () method adds a counter to an iterable and returns it in the form of an enumerating object. This enumerated object can then be used directly for loops or converted into a list of tuples using the list() function.
Syntax: enumerate(iterable, start=0)
Parameters:
- Iterable: any object that supports iteration
- Start: the index value from which the counter is to be started, by default it is 0
Return: Returns an iterator with index and element pairs from the original iterable
Example
Here, we are using the enumerate() function with both a list and a string. Creating enumerate objects for each and displaying their return types. It also shows how to change the starting index for enumeration when applied to a string, resulting in index-element pairs for the list and string.
Python3
l1 = [ "eat" , "sleep" , "repeat" ]
s1 = "geek"
obj1 = enumerate (l1)
obj2 = enumerate (s1)
print ( "Return type:" , type (obj1))
print ( list ( enumerate (l1)))
print ( list ( enumerate (s1, 2 )))
|
Output:
Return type: <class 'enumerate'>
[(0, 'eat'), (1, 'sleep'), (2, 'repeat')]
[(2, 'g'), (3, 'e'), (4, 'e'), (5, 'k')]
Using Enumerate Object in Loops
Enumerate() is used with a list called l1. It first prints tuples of index and element pairs. Then it changes the starting index while printing them together. Finally, it prints the index and element separately, each on its own line.
Python3
l1 = [ "eat" , "sleep" , "repeat" ]
for ele in enumerate (l1):
print (ele)
for count, ele in enumerate (l1, 100 ):
print (count, ele)
for count, ele in enumerate (l1):
print (count)
print (ele)
|
Output:
(0, 'eat')
(1, 'sleep')
(2, 'repeat')
100 eat
101 sleep
102 repeat
0
eat
1
sleep
2
repeat
Accessing the Next Element
In Python, the enumerate() function serves as an iterator, inheriting all associated iterator functions and methods. Therefore, we can use the next() function and __next__() method with an enumerate object.
To access the next element in an enumerate object, you can use the next() function. It takes the enumerate object as input and returns the next value in the iteration.
Python3
fruits = [ 'apple' , 'banana' , 'cherry' ]
enum_fruits = enumerate (fruits)
next_element = next (enum_fruits)
print (f "Next Element: {next_element}" )
|
Output:
Next Element: (0, 'apple')
You can call next() again to retrieve subsequent elements:
Python3
fruits = [ 'apple' , 'banana' , 'cherry' ]
enum_fruits = enumerate (fruits)
next_element = next (enum_fruits)
print (f "Next Element: {next_element}" )
|
Output:
Next Element: (0, 'apple')
Each time the next() is called, the internal pointer of the enumerate object moves to the next element, returning the corresponding tuple of index and value.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or have more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Sep, 2023
Like Article
Save Article