Currently I am learning list in python so how do we use enumerate function in python?
How do you enumerate() an item in a list Python? Can anyone explain me with an example program that how can we enumerate an item in a list in python language?
Sign Up to our social questions and Answers Engine to ask questions, answer people's questions, and connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Venkata Sai G M
When using the iterators, we need to keep track of the number of items in the iterator. This is achieved by an in-built method called enumerate(). The enumerate() method adds counter to the iterable. The returned object is a enumerate object.
Syntax : enumerate(iterable, start=0)
Example program with item in a list Python
input:
grocery = [‘bread’, ‘milk’, ‘butter’]
enumerateGrocery = enumerate(grocery)
print(type(enumerateGrocery))
# converting to list
print(list(enumerateGrocery))
# changing the default counter
enumerateGrocery = enumerate(grocery, 10)
print(list(enumerateGrocery))
output:
[(0, ‘Tue’), (1, ‘Thu’), (2, ‘Mon’), (3, ‘Wed’)]
[(5, ‘Tue’), (6, ‘Thu’), (7, ‘Mon’), (8, ‘Wed’)]