Python’s Enumerate Function
This is Python’s built-in function which is used to count an iterable and return it as an enumerated object.
Syntax:
enumerate(iterable, start=0)
Scope:
To iterate over an iterable data type and return an enumerated object (iterator) that yields tuples of (index, value), whose index starts from 0 by default.
Example:
lang_order = [‘Python’, ‘Java’, ‘C++’, ‘C’, ‘COBOL’]
print(list(enumerate(lang_order)))
Purpose: To simplify counting iterables by eliminating manual counting.
Get Hariharan G’s stories in your inbox
Join Medium for free to get updates from this writer.
Syntax used in for loop
for index, value in enumerate(iterable):
print(index, value)
Example:
Without enumerate function
i=0
for order in lang_order:
print(i, order)
i +=1
With enumerate function
for i, order in enumerate(lang_order):
print(i, order)






