What is enumerate() in Python with an example?

The enumerate() function in Python is a built-in function that provides an easy way to iterate over a sequence (such as a list, tuple, or string) while keeping track of the index of the current item. It returns an iterator that generates tuples of the form (index, element).

The syntax for enumerate() is as follows:

enumerate(iterable, start=0)Code language: Python (python)
  • iterable: This is the sequence (list, tuple, string, etc.) that you want to iterate over.
  • start: (Optional) This parameter specifies the starting value of the index. The default value is 0, but you can specify a different integer value if you want the index to start from a different number.

Here’s an example of how to use enumerate():

fruits = ['apple', 'banana', 'orange', 'grape']

for index, fruit in enumerate(fruits):
    print(f"Index: {index}, Fruit: {fruit}")
Code language: Python (python)

Output:

Index: 0, Fruit: apple
Index: 1, Fruit: banana
Index: 2, Fruit: orange
Index: 3, Fruit: grape
Code language: Python (python)

In this example, the enumerate() function is used in the for loop to iterate over the fruits list. In each iteration, the index variable holds the current index, and the fruit variable holds the value of the current fruit from the list.

enumerate() is particularly useful when you need to iterate over a sequence and also need to know the corresponding index of each item. It saves you from manually managing a separate index variable and makes your code more concise and readable.

How do you enumerate a list in Python for loop?


In Python, you can use the built-in enumerate() function to iterate through a list and get both the index and the corresponding element in each iteration. The enumerate() function returns a tuple of (index, element).

Here’s the basic syntax for using enumerate() in a for loop:

my_list = [10, 20, 30, 40, 50]

for index, element in enumerate(my_list):
    print(f"Index: {index}, Element: {element}")Code language: Python (python)

In this example, my_list contains five elements, and the enumerate() function is used in the for loop to iterate over each element in the list. In each iteration, the index variable will hold the index of the current element, and the element variable will hold the value of the current element.

The output will be:

Index: 0, Element: 10
Index: 1, Element: 20
Index: 2, Element: 30
Index: 3, Element: 40
Index: 4, Element: 50
Code language: Python (python)

As you can see, the loop has enumerated the list and printed both the index and the corresponding element for each iteration. This can be useful when you need to keep track of the index while iterating over the elements of the list.

Read More;

  • Yaryna Ostapchuk

    I am an enthusiastic learner and aspiring Python developer with expertise in Django and Flask. I pursued my education at Ivan Franko Lviv University, specializing in the Faculty of Physics. My skills encompass Python programming, backend development, and working with databases. I am well-versed in various computer software, including Ubuntu, Linux, MaximDL, LabView, C/C++, and Python, among others.

    View all posts

Leave a Comment