Sitemap
Adinda Pc

Adinda Pc

Make your script more Pythonic with enumerate()

4 min readAug 19, 2025

--

If you have ever written a Python program, you have probably found yourself in this situation: you want to loop through a list, but you also need the index of each element.

A classic way to handle this is by using range(len()) :

stationeries = ["pen","pencil","eraser"]
for i in range(len(stationeries)):
print(i, stationeries[i])

Output:

0 pen
1 pencil
2 eraser

This code works just fine, but it is not the cleanest way to write Python code. There is a more Pythonic way to do it, which is by using enumerate() .

What is enumerate()?

enumerate() is a built-in Python function that lets you iterate over an iterable (such as a list, tuple, or string) while also keeping track of the index.

Syntax:

enumerate(iterable, start=0)

Parameters:

  • Iterable: any object that supports iteration.
  • Start: the index value to begin counting from (default is 0 ).

enumerate() returns an enumerate object, which produces pairs of (index, value) as you iterate through it.

Let’s rewrite our earlier example using enumerate() :

stationeries = ["pen","pencil","eraser"]
for i, stationery in enumerate(stationeries):
print(i, stationery)

Output:

0 pen
1 pencil
2 eraser

Notice how much cleaner it looks compared to range(len()) .

If you want the index to start from 1, simply set the start parameter:

for i, stationery  in enumerate(stationeries, start=1):
print(i, stationery)

Output:

1 pen
2 pencil
3 eraser

Variations of enumerate()

You will see how useful enumerate() is when you start using it in different contexts. Let’s walk through some examples.

1. Iterating over a string

Strings are iterable too, so enumerate() works just as well with them:

for i, char in enumerate("Hello, World!"):
print(i, char)

Output:

0 H
1 e
2 l
3 l
4 o
5 ,
6
7 W
8 o
9 r
10 l
11 d
12 !

2. Using enumerate() in list comprehensions

You can combine enumerate() with list comprehension for more compact code:

stationeries = ["pen","pencil","eraser"]
indexed = [f"{i}: {stationery}" for i, stationery in enumerate(stationeries, start=1)]
print(indexed)

Output:

['1: pen', '2: pencil', '3: eraser']

3. Filtering with index

Sometimes, you do not just want the values, but you also want to filter them based on certain conditions and keep track of their positions.

Get Adinda Pc’s stories in your inbox

Join Medium for free to get updates from this writer.

For example, let’s say we want to return the indices of song titles with fewer than 5 characters:

# Returns the index of song title with length below 5
titles = ["Chroma Drift", "Rizz", "Dash", "Island"]
title_index = [i for i, title in enumerate(titles) if len(title) < 5]
print(title_index)

Output:

[1, 2]

4. Iterating through dictionaries

You can also use enumerate() with dictionaries.

Iterating keys only:

myDict = {"a": 100, "b": 200, "c": 350}

for i, key in enumerate(myDict):
print(i, key)

Output:

0 a
1 b
2 c

Iterating keys and values using .items() :

for i, (key, value) in enumerate(myDict.items(), start=1):
print(i, key, value)

Output:

1 a 10
2 b 20
3 c 30

Iterating values only using .values() :

for i, value in enumerate(myDict.values(), start=1):
print(i, value)

Output:

1 100
2 200
3 350

5. Combining enumerate() with zip()

When looping over multiple lists together, zip() is often used. But sometimes you also need the index:

foods = ["Rendang", "Nasi Goreng", "Sate Ayam"]
prices = [30000, 15000, 23000]

for i, (food, price) in enumerate(zip(foods, prices), start=1):
print(f"{i}. {food} - {price}")

Output:

1. Rendang - 30000
2. Nasi Goreng - 15000
3. Sate Ayam - 23000

6. Creating a dictionary with indices as keys

Another trick for building dictionaries with indices as keys:

stationeries = ["pen","pencil","eraser"]
stationery_dict = dict(enumerate(stationeries, start=1))
print(stationery_dict)

Output:

{1: 'pen', 2: 'pencil', 3: 'eraser'}

7. Accessing elements with next()

Since enumerate() produces an iterator of (index, value) tuples, you can use next() to retrieve items one by one:

stationeries = ["pen","pencil","eraser"]
for stationery in enumerate(stationeries):
print(stationery)
Output: 

(0, 'pen')
(1, 'pencil')
(2, 'eraser')
stationeries = ["pen","pencil","eraser"]
x = enumerate(stationeries)

# This retrieves the first index-element pair from 'x'
print(next(x)) # (0, 'pen')

print(next(x)) # (1, 'pencil')
print(next(x)) # (2, 'eraser')

Each time you call next() on the enumerate object, it advances to the following item and returns its (index, value) pair.

Conclusions

enumerate() is one of those small but powerful tools in Python that can make your code cleaner, easier to read, and more efficient. Instead of using range(len()) , you can rely on enumerate() to handle both index and the element at the same time.

From now on, whenever you find yourself writing a loop where you need both the index and the value, think of enumerate() .

References

--

--