Questions And Answers

More Tutorials

Python Enumerate Function

What enumerate function does is, it assigns an index to every element or value in the object that we want to iterate, so we do not have to assign a specific variable for incremental function, instead we have to apply a for loop, and our function will start working. Its syntax is a lot simpler and shorter than what we have been following till now.
enumerate(iterable, start=0)

When calling a simple enumeration function, we have to provide two parameters:

The data structure that we want to iterate.
The index from where we want to start our iteration.

Note: The iterable must be an object that supports iteration.

Example of enumerate using a python list.

We can iterate over the index and value of an item in a list by using a basic for loop with enumerate().
for index,val in enumerate(list_1):


0 code
1 with
2 mockstacks

Using Enumerate() on a list with start Index:

In the below example, the starting index is given as 5. The index of the first item will start from the given starting index.


list_2 = ["Python", "Programming", "Is", "Fun"]
#Counter value starts from 5
result = enumerate(list_2, 5)


[(5, 'Python'), (6, 'Programming'), (7, 'Is'), (8, 'Fun')]

If we do not provide the index, we want to start the iteration from then it automatically starts its iteration from zero index i.e., the beginning of the data structure.

Instead of returning a string, the enumerate function returns an object by adding the iterating counter value. We can also convert the enumerator object into a list(), tuple(), set(), and many more.

Advantages of using Enumerate:

It is a built-in function
It makes the code shorter
We do not have to keep count of the number of iterations
It makes the implementation of for loop simpler and cleaner
Lesser code so lessor chances of error and bugs
We can loop through string, tuple or objects using enumerate
We can start the iteration from anywhere within the data structure as we have the option of providing the starting index for iteration.


l1 = ["Bhindi", "Aloo", "chopsticks", "chowmein"]

# i = 1
# for item in l1:
#     if i%2 is not 0:
#         print(f"Jarvis please buy {item}")
#     i += 1

for index, item in enumerate(l1):
    if index%2==0:
        print(f"Jarvis please buy {item}")



In this page (written and validated by ) you learned about Python Enumerate Function . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python If name main usage and necessity.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.