In Python, enumerate() is a built-in function that converts an iterable object into an enumerate object, which contains both the index and corresponding value of each element.
Function Syntax
enumerate(iterable, start=0)
Parameters:
iterable: Can be any iterable object, including lists, tuples, sets, strings, and dictionaries.start: Specifies the starting value for the index, defaults to0.
The enumerate() function returns an iterator consisting of tuples, where each tuple contains two elements: the first is the element's index, and the second is the element's value.
enumerate() Function Examples
Here's an example using the enumerate() function:
fruits = ['apple', 'banana', 'cherry']
for index, fruit in enumerate(fruits):
print(index, fruit)
Program Output
0 apple 1 banana 2 cherry