What Does the Python enumerate() Function Do, and How Do You Use It?

Python supports many iterable objects: lists, tuples, and strings are just a few. Python’s for loop is flexible enough to handle these objects with ease. In particular, it saves you from dealing with the numeric index of each entry yourself.

But sometimes, you’ll want to work with an index, to count or filter items in a collection for example. Learn how you can use the Python enumerate function to iterate over a sequence while keeping track of both the index and the element.

4

Iterating Without enumerate()

Consider the following Python list.

Suppose you want to keep track of the count of each element in the list. You coulduse a for loopwith arange()function like this.

Spotify Daylist on an iPad with AirPods nearby

While this works, it has a few shortcomings:

A better solution exists: the enumerate() function.

pc built by chatgpt internal fans.

How enumerate() Works in Python

The Pythonenumerate()function takes a data collection and returns an enumerate object. The enumerate object contains a counter as a key for each item the object contains. Theenumerate()function does so by assigning each item a count. This count corresponds to the number of iterations the function has completed to reach that value.

This counter makes it easier to access the items in the collection or even mutate the collection where possible.

BBC iPlayer showing on a smart TV.

Withenumerate(), you do not have to keep track of the length of the loop or the iteration count. you’re able to also avoid explicitly accessing the value using the index operator, likefruits[count]. The enumerate function automatically handles all these features.

The Syntax of Python enumerate()

Below is the general syntax for the enumerate() function.

enumerate() takes two arguments:

Worms eye view of spiral stained glass decors through the roof

How enumerate() Is Implemented

To understand howenumerate()works, let’s see how it is actually implemented.

This function, which you could find in thePython enumerate documentation, takes a sequence and a start argument which defaults to 0.

The function then initializes the n variable to the start variable. This keeps track of the number of iterations.

In the for loop that follows for each element in the sequence,

The return value is an object of key-value pairs.

If you were to call theenumerate()function and pass it the list of fruits, this would be the output value.

Enumerate With a Different Starting Index

In the above example, we did not specify the starting index. The example below shows you how you could set the starting index to a number of your choice.

To start at 10, do the following.

Python enumerate() Examples

Here are some examples showing how you could useenumerate()in Python.

Enumerate a Python String

In this example, you will use enumerate to retrieve the letter at index 5:

Enumerate a Python Tuple

You can also useenumerate()to iterate over aPython tuple. In this example, the program prints a header in the first iteration, before displaying the items.

Enumerate a Python List

In the example below, you are using enumerate to decrement all the numbers in the list above 10 by 2.

Use enumerate to Count Your Iterables

Python includesenumerate()as a built-in function. you may use it to iterate and count items in an iterable collection such as list, tuple, or string. It can help you keep track of the true index of each element, but you can also specify your own starting index. Use it as an alternative to therange() function if you are working with objects that are not countable and indexable objects.

Using loops in lists can make your code even more efficient. This tutorial shows you how to start using list loops in Python.

The key is not to spook your friends with over-the-top shenanigans.

Anyone with more than a passing interest in motorsports must see these films.

You’re not getting the most out of what you pay for iCloud+.

Your phone’s camera app doesn’t show this, so it’s easy to miss.

Now, I actually finish the books I start.

Technology Explained

PC & Mobile