Python enumerate() Function with examples
In this tutorial, you will learn about Python enumerate() Function with examples.
While we are working with decorates, we need to keep track of iterations. This problem solved by inbuilt function in python that is enumerate().
This enumerate() function takes a collection (e.g. a tuple or list) and returns it as an enumerate object.
Syntax:
enumerate(iterable, start)
Parameter Values : iterable An iterable object
Parameter Description: start A Number. Defining the start number of the enumerate object. Default 0
Example program on enumerate() function:
# Python program to illustrate
# enumerate function
l1= ["work","do","morework"]
t1 = ('food','love','money')
# creating enumerate objects
obj1 = enumerate(l1)
obj2 = enumerate(t1)
print("Return type:",type(obj1))
print(list(enumerate(l1)))
print("Return type:",type(obj2))
print(list(enumerate(t1)))
# changing start index to 2 from 0
print(list(enumerate(l1,2)))
Output:
Return type: <class 'enumerate'> [(0, 'work'), (1, 'do'), (2, 'morework')] Return type: <class 'enumerate'> [(0, 'food'), (1, 'love'), (2, 'money')] [(2, 'work'), (3, 'do'), (4, 'morework')]