enumerate() Python function is built-in.
Enumerate() allows you to loop over a list and keep track of the running index using a count variable.
The Python programming language has several useful features that can help you sort through and manipulate data. One of these features, which people often forget about is the
enumerate() Python function.
enumerate() Python function allows you to loop over a list of items while keeping track of the index value in a separate variable. For example, let’s say you have a list of names that you want to loop through. You also want to keep track of the index values of those names. You could use
enumerate() to achieve this goal.
In this tutorial, we are going to break down how to use the
enumerate() function in Python, and discuss why it’s useful.
Python Iterators Refresher
Iteration is where you go through a list of items or data in programming. In context, when you loop through a list of employee names, you are iterating through the list. When you create a
for loop or another type of loop in Python, you iterate code through a set of values.
One of the most common iteration procedures is to go through a list based on its length. Let’s say you want to print out a list of employee names from an array. You may use this code:
employee_names = ["Paul", "John", "Abbie", "Charlotte", "Ron"] for n in range(0, len(employee_names)): print(n, employee_names[n])
Our code returns the following:
0 Paul 1 John 2 Abbie 3 Charlotte 4 Ron
Let’s break down our code. On the first line, we declare a variable—
employee_names—that holds a list of our employee names. Then, on the next line, we create a
for loop that iterates through our list of employee names.
This loop will execute equal to the number of items in the employee_names array, because of our range(
len(employee_names)) statement. On the next line, our program prints out the index number, as well as the employee name, which has that index number.
This is an iterative function. However, we could improve on it. That’s where the
enumerate() function comes in.
For more on Python functions, syntax, and variables check out our other Python tutorials.
In our above example, we created a range() statement that was equal to the length of our array of employee names. But if we wanted to make our code more readable, we may wish to use the enumerate() function instead.
enumerate() class object generates the element indexes for a loop, so you don’t have to worry about creating a
range() statement and then getting the length of an array. For our code to work, we need to pass any object that supports iteration through our
The enumerate function keeps track of two values: the index and the value of the item. So, instead of having to reference
employee_names[n] as we did in the first example, we can use
name. Here is the syntax of enumerate:
for index, value in enumerate(array_name): // Code here
Let’s use an example to illustrate how
enumerate() works. Here is an example of the
enumerate() function in action:
employee_names = ["Paul", "John", "Abbie", "Charlotte", "Ron"] for index, name in enumerate(employee_names): print(index, name)
Here’s what our program returns:
0 Paul 1 John 2 Abbie 3 Charlotte 4 Ron
As you can see, our output is the same as our function above, but we are using the
enumerate() function instead of
enumerate() function tells our program that we want to loop through our
employee_names array, and does the work for us. Our iterator is returned by enumerate, and the enumerate function starts the index at 0.
In our example, we have not specified an index value, so our
enumerate() function starts at
. If we want our program to start at a particular index, we can use the optional argument to define an index value. Here’s an example of an
enumerate() function starting at
employee_names = ["Paul", "John", "Abbie", "Charlotte", "Ron"] for index, name in enumerate(employee_names, 1): print(index, name)
Our program returns the following:
1 Paul 2 John 3 Abbie 4 Charlotte 5 Ron
As you can see, our list enumerate function returns a list of our names with the number of iterations performed on our array, starting with
1, as we defined above.
You can also create tuples with the index and list item using the
enumerate() function. Here’s an example of this in action:
employee_names = ["Paul", "John", "Abbie", "Charlotte", "Ron"] employee_list_with_counter = list(enumerate(employee_names, 1)) print(employee_list_with_counter)
Our code returns a list of tuples with the employee names, as well as the counter number from our
[(1, 'Paul'), (2, 'John'),(3, 'Abbie'), (4, 'Charlotte'), (5, 'Ron')]
That’s how you use
enumerate() in Python. Python
enumerate() is a built-in Python function that allows you to loop over an iterable object and keep track of the running index using a count variable. Enumerate is particularly useful if you have an array of values that you want to run through entirely.
Now you’re able to iterate and use
enumerate() like an expert.