All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Enumerate For Loop

Last Updated : Jul 1, 2023

Python Enumerate For Loop

In this tutorial we will show you the solution of python enumerate for loop, when working as a programmer, you may need to use a loop to iterate through an iterable object.

This means you don’t need a counting variable to access items in the iterable. Sometimes, though, you do want to have a variable that changes on each loop iteration. So let’s start.

Step By Step Guide On Python Enumerate For Loop :-

A for loop in Python uses collection-based iteration. This means that Python assigns the next item from an iterable to the loop variable on every iteration.

You want to print the index of the item in the list to the screen on every iteration, in addition to the value itself.

One way to approach this task is to create a variable to store the index and update it on each iteration.

A common mistake in this approach would be to forget to increment the variable that stores the index value.

You can use enumerate() in a loop in almost the same way that you use the original iterable object.

This function keeps track of the iterations so you don't have to remember to update the variable that stores the value of the index.

foods = [
    "Noodles",
    "Soup",
    "Pizza",
    "Doughnut",
    "Cake",
    "Chocolate milk shake"
]
for food in foods:
    print(food)
print()
index = 0
for food in foods:
    print(index, food)
    index += 1
print()
for index, food in enumerate(foods, start=1):
    print(index, food)
  1. The first line has an array named foods, which contains six strings of different food names.
  2. The next line has a for loop in which there is an iterable variable named food for individual food items from the items.
  3. This food variable will store the values of all the items in the array. In the next line there is a print statement which will display the value of the food variable at each iteration.
  4. In the next line there is an empty print statement, which will result in a blank line in the output window.
  5. In the next line, there is a variable named index which has the value 0 assigned to it.
  6. In the next line, there is another for loop which is similar to the for loop mentioned above with a slight difference, which is in the print statement inside the loop.
  7. The print statement contains two arguments, which means the value of both variables will be displayed on the same line.
  8. The print statement will display the value of the index variable as well as the value of the food variable at each iteration.
  9. In the next line, the value of the index variable will be incremented at each iteration by 1.
  10. But a common mistake that can be made by any programmer while using this approach is to forget to increment the value of the index variable. So, at every iteration, the value of the index will be the same, which is 0.
  11. You can use Python’s enumerate() function to overcome this problem of forgetting to increment the value of the index variable.
  12. You have to modify the iterable variable to use the enumerate() function, which requires you to specify both the iterable variable and the index variable.
  13. The enumerate() function can accept two arguments: one is the array or the list, and the second one is an optional start parameter. The start parameter will represent the starting value of the index variable.
  14. Then you can pass both the variables to the print statement to display their value at each iteration.

Conclusion :-

So finally, in conclusion, we can say that with the help of this tutorial, you can now use the enumerate() function in a for loop in a Python program.

You can use the other methods as well in a for loop, but it is more convenient to use the enumerate() function in a Python program.

I hope this tutorial on python enumerate for loop helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪