Skip to main content

Command Palette

Search for a command to run...

Enumerate Function in Python

A brief explanation about the built-in enumerate function in Python

Updated
3 min readView as Markdown
Enumerate Function in Python
S
I build tools that make developers faster and help startups grow. I specialize in building agentic workflows, oss infrastructure, high-performance web systems, and creating technical content. I've spent years building products used by thousands, from developer-facing infrastructure at Keploy and Wizdom to building the user base for early-stage startups. My work sits at the intersection of agentic AI, open-source ecosystems, and growth engineering.

Introduction

The enumerate function in Python converts a data collection object into an enumerate object. Enumerate returns an object that contains a counter as a key for each value within an object, making items within the collection easier to access.

Enumerate Function

The enumerate function is a built-in function in Python that allows you to loop over a sequence (such as a list, tuple, or string) and get the index and value of each element in the sequence at the same time. Here's a basic example of how it works:

# Loop over a list and print the index and value of each element
fruits = ['apple', 'banana', 'mango']
for index, fruit in enumerate(fruits):
    print(index, fruit)

The output of this code will be:

0 apple
1 banana
2 mango

As you can see, the enumerate function returns a tuple containing the index and value of each element in the sequence. You can use the for loop to unpack these tuples and assign them to variables, as shown in the example above.

Changing the start index

By default, the enumerate function starts the index at 0, but you can specify a different starting index by passing it as an argument to the enumerate function:

# Loop over a list and print the index (starting at 1) and value of each element
fruits = ['apple', 'banana', 'mango']
for index, fruit in enumerate(fruits, start=1):
    print(index, fruit)

The output will be:

1 apple
2 banana
3 mango

The enumerate function is often used when you need to loop over a sequence and perform some action with both the index and value of each element. For example, you might use it to loop over a list of strings and print the index and value of each string in a formatted way:

fruits = ['apple', 'banana', 'mango']
for index, fruit in enumerate(fruits):
    print(f'{index+1}: {fruit}')

And, the output is,

1: apple
2: banana
3: mango

In addition to lists, you can use the enumerate function with any other sequence type in Python, such as tuples and strings. Here's an example with a tuple:

# Loop over a tuple and print the index and value of each element
colors = ('red', 'green', 'blue')
for index, color in enumerate(colors):
    print(index, color)

And here's an example with a string:

# Loop over a string and print the index and value of each character
s = 'hello'
for index, c in enumerate(s):
    print(index, c)

Conclusion

Well, that's a wrap for now!! Hope you folks have enriched yourself today with lots of known or unknown concepts. I wish you a great day ahead and till then keep learning and keep exploring!!

S
Sam Maji3y ago

Helpful

4
S

Keep Exploring!!

2

Python : Advanced

Part 10 of 19

In this series, I will be discussing about some complicated/under-used features of Python, important libraries such as pandas; and also OOPs, Data Analytics and AI-ML.

Up next

OS Modules in Python

The OS module in Python is a built-in library that provides functions for interacting with the operating system.