Python’s enumerate() function demystified

Hey there,

Python’s enumerate function is a mythical beast—

It’s hard to summarize its purpose and usefulness in a single sentence.

And yet, it’s a super useful feature that many beginners and even intermediate Pythonistas are blissfully unaware of.

Basically, enumerate() allows you to loop over a collection of items while keeping track of the current item’s index in a counter variable.

Let’s take a look at a quick example:

names = ['Bob', 'Alice', 'Guido']
for index, value in enumerate(names):
    print(f'{index}: {value}')


This produces the following output:
 

0: Bob
1: Alice
2: Guido


As you can see, this iterated over the names list and generated an index for each element by increasing a counter variable starting at zero.

Now why is keeping a running index with the enumerate function useful?

I noticed that new Python developers coming from a C or Java background sometimes use the following range(len(...)) antipattern to keep a running index while iterating over a list with a for-loop:
 

# HARMFUL: Don't do this
for i in range(len(my_items)):
    print(i, my_items[i])


By using the enumerate function skillfully, like I showed you in the “names” example above, you can make this looping construct much more “Pythonic” and idiomatic.

You see, there’s usually no need to generate element indexes manually in Python—you simply leave all of this work to the enumerate function.

And as a result your code will be easier to read and less vulnerable to typos.

Another useful feature is the ability to choose the starting index for the enumeration. 

The enumerate() function accepts an optional argument which allows you to set the initial value for its counter variable:

names = ['Bob', 'Alice', 'Guido']
for index, value in enumerate(names, 1):
    print(f'{index}: {value}')


In the above example I changed the function call to enumerate(names, 1) and the extra 1 argument now starts the index at one instead of zero:

1: Bob
2: Alice
3: Guido


And voilà, this is how you switch from zero-based indexing to starting with index 1 (or any other int, for that matter) using Python’s enumerate() function.

You might be wondering how the enumerate function works behind the scenes—so let's talk about that for a bit: 

Part of it’s magic lies in the fact that enumerate is implemented as a Python iterator.

This means that element indexes are generated lazily (one by one, just-in-time), which keeps memory use low and keeps this construct so fast.

Let’s play with some more code to demonstrate what I mean:
 

>>> names = ['Bob', 'Alice', 'Guido']
>>> enumerate(names)
<enumerate object at 0x1057f4120>


In the above code snippet I set up the same enumeration you’ve already seen in the previous examples.

But instead of immediately looping over the result of the  enumerate call I’m just displaying the returned object on the Python console.

As you can see, it’s an “enumerate object.”

This is the actual iterator.

And like I said, it generates its output elements lazily and one by one when they’re requested.

In order to retrieve those “on demand” elements so we can inspect them, I’m going to call the built-in list() function on the iterator:
 

>>> list(enumerate(names))
[(0, 'Bob'), (1, 'Alice'), (2, 'Guido')]


For each element in the input list (names) the iterator returned by enumerate() produces a tuple of the form (index, element).

In your typical for-in loop you’ll use this to your advantage by leveraging Python’s data structure unpacking feature:
 

for index, element in enumerate(iterable):
    # ...

And there you have it—enumerate() is awesome!

Let's do a quick recap:

    1. "enumerate()" is a built-in function of Python. You use it to loop over an iterable with an automatic running index generated by a counter variable.

    2. The counter starts at 0 by default, but you can set it to any integer.

    3. enumerate was added to Python starting at version 2.3 with the implementation of PEP 279.

    4. Python’s enumerate function helps you write more Pythonic and idiomatic looping constructs that avoid the use of clunky and error-prone manual indexing.


Happy Pythoning!

— Dan Bader

Older messages

[Python Dependency Pitfalls] "Re-inventing the wheel" disease

Wednesday, February 17, 2021

Hey there, PyPI, the Python packaging repository, now contains more than 100000 third-party packages in total. That's an *overwhelming* number of packages to choose from... And this feeling of

[Python Dependency Pitfalls] What dev managers expect from Python candidates

Monday, February 15, 2021

Hey there, My friend Og is a senior manager at Red Hat and works with a large team of developers and quality engineers using Python. I got to pick his brain on what he thought were the most important

[Python Dependency Pitfalls] How to set the world on fire

Sunday, February 14, 2021

Hey there, #1 on my list of dependency management pitfalls is there for a good reason: It lead to a single developer causing mayhem and breaking thousands of open-source projects around the world in

[PythonistaCafe] Q&A

Saturday, February 13, 2021

Hey there, At this point you should have a pretty good idea of what PythonistaCafe is about and what makes it special. In this email I want to answer some common questions that I get asked about the

A story about crushed coding dreams

Saturday, February 13, 2021

Hey there, The other day I read this story of a fledgling Python coder named Seth on Reddit: Seth's been learning Python for 5 months, and things have been "hit and miss" for a while now.

You Might Also Like

Check Out These Awesome Gifts!

Thursday, November 28, 2024

Let us help you check one thing off your to-do list with this guide to the best gifts for the holiday season. Make the holidays a little brighter with these great gift options. From health and wellness

🚀 Accelerate Your Growth As a Software Architect

Thursday, November 28, 2024

What students are saying about my courses More than 4300+ students already completed my courses. And they gave them a 4.9/5 ⭐ rating. I'd love to see your success story next on this wall of

🫵 Android Developer Previews Are Not For You — Virtual Reality Might Finally Be Socially Acceptable

Wednesday, November 27, 2024

Also: iPhone Camera vs. Digital SLR, and More! How-To Geek Logo November 27, 2024 Did You Know The band Radiohead was originally called "On a Friday"—the band was formed by high school

JSK Daily for Nov 27, 2024

Wednesday, November 27, 2024

JSK Daily for Nov 27, 2024 View this email in your browser A community curated daily e-mail of JavaScript news JavaScript Certification Black Friday Offer – Up to 54% Off! Certificates.dev, the trusted

Ranked | The World's Biggest Importers of Goods 🌎

Wednesday, November 27, 2024

As Trump tariffs are anticipated to disrupt global trade and push up prices for consumers, we show the world's biggest importers of goods. View Online | Subscribe | Download Our App >> 📱Book

Daily Coding Problem: Problem #1621 [Easy]

Wednesday, November 27, 2024

Daily Coding Problem Good morning! Here's your coding interview problem for today. This problem was asked by Google. A regular number in mathematics is defined as one which evenly divides some

Spyglass Dispatch: The Xitter Bail Out • OpenAI Tender • Grokking Grok • Smartphone Sales • Fischer Random Chess • Scott Bessent for Treasury

Wednesday, November 27, 2024

The Xitter Bail Out • OpenAI Tender • Grokking Grok • Smartphone Sales • Fischer Random Chess • Scott Bessent for Treasury The Spyglass Dispatch is a free newsletter sent out daily on weekdays. Feel

The Long Road Home: A Story of Loss, Learning, and Renaissance - PART 4

Wednesday, November 27, 2024

Top Tech Content sent at Noon! How the world collects web data Read this email in your browser How are you, @newsletterest1? 🪐 What's happening in tech today, November 27, 2024? The HackerNoon

Top Tech Deals 🏷️ PS5 Slim, 4K TVs, 10th Gen iPad, and More!

Wednesday, November 27, 2024

The Black Friday madness is here! How-To Geek Logo November 27, 2024 Top Tech Deals: PS5 Slim, 4K TVs, 10th Gen iPad, and More! The Black Friday madness is here! Black Friday sales are here, and we

The 165+ best Black Friday deals

Wednesday, November 27, 2024

Windows Super God Mode; Bluesky starter packs; Tech gifts under $100 -- ZDNET ZDNET Tech Today - US November 27, 2024 Black Friday 2024 live blog Best Black Friday deals 2024: 165+ sales live now