The enumerate() function in Python provides a concise syntax to access the items in an iterable along with their indices. We’ll start by reviewing how to access items and indices using simple looping and then proceed to learn the syntax of Python’s enumerate() function. We’ll also code examples along the way. Let’s begin.

How to Iterate Using for Loop in Python

Any Python object that you can iterate over and access the individual items—one at a time is called an iterable. Therefore, Python lists, tuples, dictionaries, and strings are all iterables. Let’s take an example of a shopping list defined in the code cell below. In Python, you can use the for loop to loop through any iterable. The syntax to do this is as follows: Now, using this syntax, let’s loop through shopping_list and access the individual items. This construct helps you access the items directly. However, you may sometimes need to access the index of the items, in addition to the items themselves. You can use an index variable, which you can increment inside the loop body, as shown below: But this is not the most efficient method. If you don’t remember to increment the index, your code will not work as expected. Another common pattern of using the for loop is in conjunction with the range() function. We’ll learn about that in the next section.

How to Use range() Function to Access Index

Python’s built-in len() function returns the length of any Python object. So you can call the len() function with shopping_list the argument to get the length of the shopping_list (which is 5, in this case). The range() function returns a range object which you can then use in looping. When you loop through range(stop), you get the indices 0, 1, 2,…, stop-1. By setting stop = len(list), you can get the list of valid indices. So by using the range() function, you can access the index as well as the corresponding item, as shown below. However, this is not the recommended Pythonic way to access both indices and items simultaneously.

Syntax of Python enumerate() Function

Python’s enumerate() function lets you access the items along with their indices using the following general syntax: In the above syntax:

is a required parameter, and it can be any Python iterable, such as a list or tuple.start is an optional parameter that controls the index at which the counting starts. If you don’t specify the value of start, it defaults to zero.

You can now call the enumerate() function with shopping_list and it returns an enumerate object, as shown in the code cell below. You cannot loop through the enumerate object. So let’s convert the enumerate object into a Python list. Another method to access the items of the enumerate object is to call the next() function with the enumerate object as the argument. In Python, the next() function returns the next item from an iterator. Internally, the next() function works by calling the next method on the iterator object to retrieve the successive items. Let’s assign the enumerate object to the variable shopping_list_enum. When you first call next() with shopping_list_enum, you’ll get index zero and the item at index 0: the tuple (0, ‘fruits’). When you continue to make further calls to the next() function, you’ll get the successive items along with their indices, as described below. What happens if you call next() function after you’ve accessed all items and have reached the end of the list? You get a StopIteration Error. The enumerate() function returns the successive indices and items only when needed and are not computed ahead of time. In Python projects where you need to take memory efficiency into account, you can try using the enumerate() function when you need to loop through large iterables.

Python enumerate() Function Examples

Now that we’ve learned the syntax to use the enumerate() function, let us modify the for loop we had earlier. From the previous section, we know that looping through the enumerate object returns a tuple with the index and the item. We can unpack this tuple into two variables: index and item. Next, let’s see how to specify a custom start value.

How to enumerate() with Custom Start Value

Python follows zero indexing, so the start value is 0 by default. However, if you need the indices in human-readable form—starting from 1 or any other index of your choice, you can specify a custom start value. In the shopping_list example, if you’d like to start counting from 1, set start = 1. However, when specifying the custom start value, you should be sure to specify it as the second positional argument. If you mistakenly swap the order of the iterable and the start value, you would run into an error, as explained in the code cell below. To avoid such errors, you can specify start as a keyword argument, as shown below. So far, you’ve learned how to use the enumerate() function with Python lists. You can also use the enumerate function to loop through Python strings, dictionaries, and tuples.

How to Use enumerate() Function with Python Tuples

Suppose shopping_list is a tuple. In Python, tuples are also collections—similar to Python lists, but they are immutable. Therefore, you cannot modify them, and trying to do so will lead to error. The following code snippet initializes shopping_list as a tuple. If you try to modify the first item in the tuple, you will get an error as a Python tuple is an immutable collection. ▶ Now, run the following code snippet to verify that the enumerate() function works as expected with tuples.

How to Use enumerate() Function with Python Strings

You can also use the Python enumerate() function to loop through strings and access the characters and indices simultaneously. Here is an example. From the output above, we see that the characters along with their indices (0-8) have been printed out.

Conclusion👩🏽‍💻

Here’s a summary of what you’ve learned.

You can use for loops to access the items and maintain a separate counter or index variable.If you’d like, you can use the range() function to get the valid indices and access the items by indexing into the list.As a recommended Pythonic way, you can use Python’s enumerate() function with the syntax: enumerate(iterable). By default, the count or index starts at 0.You can specify the custom start value with the syntax enumerate(iterable, start) to get the index starting at the value start and the corresponding items.You can use the enumerate function when working with tuples, strings, dictionaries, and in general, any Python iterable.

I hope you found this tutorial on enumerate() function helpful. Next, learn how to find the index of an item in Python lists. Keep coding!

Python enumerate   Function  Explained with Examples - 89Python enumerate   Function  Explained with Examples - 63Python enumerate   Function  Explained with Examples - 74Python enumerate   Function  Explained with Examples - 38Python enumerate   Function  Explained with Examples - 92Python enumerate   Function  Explained with Examples - 50Python enumerate   Function  Explained with Examples - 32