Open In App

Enumerate() in Python

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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"
 
# creating enumerate objects
obj1 = enumerate(l1)
obj2 = enumerate(s1)
 
print ("Return type:", type(obj1))
print (list(enumerate(l1)))
 
# changing start index to 2 from 0
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"]
 
# printing the tuples in object directly
for ele in enumerate(l1):
    print (ele)
 
# changing index and printing separately
for count, ele in enumerate(l1, 100):
    print (count, ele)
 
# getting desired output from tuple
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.

 



Last Updated : 04 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads