Enumerate Python: Step-by-Step Guide

The Python enumerate() function adds a counter to an iterable object. The counter lets you keep track of how many iterations have occurred. This counter is stored as a separate 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.

The built-in enumerate() function allows you to loop over a list of items while keeping track of the index value in a separate variable. In this tutorial, we are going to break down how to use the enumerate() Python function, and discuss why enumerate() is useful.

Python Iterators Refresher

Iteration is a process where a particular block of code is run until an outcome is achieved. Often, the outcome is reading every item in a list.

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 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

On the first line, we declare a Python 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. The len() method retrieves how many items are in the employee_names array. The range() method creates a list of numbers equal to the length of our list. This lets us iterate over each item in our list.

Next, 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 how to learn Python guide.

How to Use the Python Enumerate Method

The enumerate() method creates a counter that tracks how many iterations have occurred in a loop. enumerate() is built-in to Python, so we do not have to import any libraries to use the enumerate() method.

If you use the enumerate method(), don’t have to worry about creating a range() statement and then getting the length of an array. enumerate() creates an object that supports iteration. This means you can iterate over enumerate() without having to create a separate iterator.

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:

employee_names = ["Paul", "John", "Abbie", "Charlotte", "Ron"]

for index, name in enumerate(employee_names):
	print(index, name)

The enumerate() function tells our program that we want to loop through our employee_names array. The enumerate() method returns two values: index and name. “index” refers to the number of iterations that have occurred. “name” is an individual item in our list.

Here’s what our program returns:

0 Paul
1 John
2 Abbie
3 Charlotte
4 Ron

Our output is the same as the output from our first example. In this example, we are using the enumerate() function instead of range() and len(). Our code is cleaner and easier to read when we use enumerate().

How the Python Enumerate Object Works

By default, enumerate() returns a list of tuples. We can see this by specifying one value after our “for” statement instead of two:

for item in enumerate(employee_names):
	print(item)

This code returns our employee names alongside the index values of each employee name in our list:

(0, 'Paul')
(1, 'John')
(2, 'Abbie')
(3, 'Charlotte')
(4, 'Ron')

In our first example, we used “name” and “index” to separate these values. We accomplished this using a technique called unpacking.

Specifying multiple variables after our “for” statement lets us “unpack” the result of the enumerate() method. The first variable, “index”, correlates with the index number in the tuple. “name” becomes the name that is stored in our tuple.

Adding a Starter Index Value

In our last example, we have not specified an index value.

As a result, our enumerate() function starts counting at 0. If we want our program to start at a particular index, we can specify a starter index value. The starter index value is an optional argument. Here’s an example of an enumerate() function starting at 1:

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

s you can see, our list enumerate function returns a list of our names. The number of iterations performed on our array, starting with 1, is also shown.

Venus profile photoVenus profile photo

“Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!”

Venus, Software Engineer at Rockbot

This demonstrates how we can manipulate the starting index for the enumerate() method. Instead of starting with 0, our counter starts at 1.

This is useful, for instance, if you are tracking a leaderboard and want to display numbers starting from 1 instead of 0.

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 enumerate() function:

[(1, 'Paul'), (2, 'John'),(3, 'Abbie'), (4, 'Charlotte'), (5, 'Ron')]

Conclusion

Python enumerate() is a built-in Python function. The enumerate() function allows you to loop over an iterable object and keep track of how many iterations have occurred. 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.

2 Ratings