Share this blog with others!

Advertisement

The Python library has a built-in method called Enumerate(). It receives a collection of tuples as input and produces an enumerated object. Enumerate() adds a counter to each item of an iterable object and delivers an enumerate object as an output string in Python.

Python Enumerate Function Syntax

enumerate(iterable, startIndex)

  • Iterable: a loopable object.
  • StartIndex: (optional) The count will begin with the number specified in startIndex for the first item in the loop and increase for each subsequent item until it reaches the loop’s end.

The count will begin at 0 if startIndex is not supplied.

Python Enumerate Return Value

The python enumerate() method returns an iterable with a counter added to it. The object returned is an enumerate object.

The list() and tuple() methods can be used to convert enumerate objects to list and tuple, respectively.

Enumerate Object in Python

In the code below, the alphabets list is passed to the Enumerate method. The Enumerate Python result is shown using the list() function.

When changing the default counter in the enumerated object, the start index is set to 2. The first item’s index will begin at the supplied start index.

alphabets = ['A', 'B', 'C', 'D']
enumeratedAlphabet = enumerate(alphabets)

print(type(enumeratedAlphabet))

# converting enumerated object to list
print(list(enumeratedAlphabet))

# changing the default counter in enumerated object
enumeratedAlphabet = enumerate(alphabets, 10)
print(list(enumeratedAlphabet))

Output:

<class 'enumerate'>
[(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D')]
[(10, 'A'), (11, 'B'), (12, 'C'), (13, 'D')]

Python Enumerate Function in Loop

Enumerating over an object with and without startIndex is demonstrated in this example.

  • Because there is no startIndex in the first for-loop, the index starts at 0.
  • The startIndex of the second for-loop is 100, thus the index begins at 100.

alphabets = ['A', 'B', 'C', 'D']

for alphabet in enumerate(alphabets):
  print(alphabet)

print('\n')

for count, alphabet in enumerate(alphabets):
  print(count, alphabet)

print('\n')
# changing default start value
for count, alphabet in enumerate(alphabets, 100):
  print(count, alphabet)

Output:

(0, 'A')
(1, 'B')
(2, 'C')
(3, 'D')

0 A
1 B
2 C
3 D

100 A
101 B
102 C
103 D

Enumerating String in Python

The string is an array in Python, so you may loop over it. If you give enumerate() a string, it will return the index and value for each character in the string.

Example:

my_str = "HackTheDeveloper"
for i in enumerate(my_str):
  print(i)

Output:

(0, 'H')
(1, 'a')
(2, 'c')
(3, 'k')
(4, 'T')
(5, 'h')
(6, 'e')
(7, 'D')
(8, 'e')
(9, 'v')
(10, 'e')
(11, 'l')
(12, 'o')
(13, 'p')
(14, 'e')
(15, 'r')

Advantages of Enumerate Function

The following are some advantages and disadvantages of using Enumerate in Python:

  • Enumerate loops through a list, tuple, dictionary, or string and returns the values as well as the index.
  • You can use list.index to get the index value in a for-loop (n). List.index(n), on the other hand, is quite expensive because it traverses the for-loop twice. Enumerate comes particularly handy in this situation because it provides both the index and the items all at once.

Learn Intermediate Python.

Learn GUI Programming in C.

Learn more about Python enumerate function from realPython.

About Author
5 1 vote
Article Rating

Share this blog with others!
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top