Why use enumerate in Python?

When should we use enumerate in Python instead of just using a regular for loop?

I like Python’s enumerate function. It lets you loop over things retrieving both the index and the value at the same time.

You can use it like this:

mylist = ["hello", "world", 123]
for index, value in enumerate(mylist):
    print(index, value)

STDOUT

0, hello
1, world
2, 123

It’s a nice Pythonic way of getting more information from an iterable.

3 Likes