The enumerate() function is a built-in function that returns an enumerate object. This lets you get the index of an element while iterating over a list.

In other programming languages (C), you often use a for loop to get the index, where you use the length of the array and then get the index using that. That is not Pythonic, instead you should use enumerate().

In Python you can iterate over the list while getting the index and value immediately.

Related course: Complete Python Programming Course & Exercises

Enumerate object

The basic syntax is is enumerate(sequence, start=0)

The output object includes a counter like so: (0, thing[0]), (1, thing[1]), (2, thing[2]),

As input it takes a sequence like a list, tuple or iterator. The start parameter is optional.
If the start parameter is set to one, counting will start from one instead of zero

Create a sequence and feed it to the enumerate function. This can be any type of sequence, in this example we use a list. Then we output the object.

Try the program below:

1
2
3
4
5
6
# create a sequence
browsers = ['Chrome','Firefox','Opera','Vivaldi']

# create an enumeratable and convert to list
x = list(enumerate(browsers))
print(x)

You should see this output:

enumerate

The returned object can be treated like an iterator: the next method call will work:

1
2
3
4
5
6
7
browsers = ['Chrome','Firefox','Opera','Vivaldi']
eObj = enumerate(browsers)

x = next(eObj)
print(x)
x = next(eObj)
print(x)

Enumerate List

Let’s see how you can enumerate a Python list. You can open the Python shell to try it out.

You can iterate over a Python list by using enumerate(). Lets see that in a simple example.

1
2
3
4
5
6
7
8
>>> fruits = [ "Apple","Berry","Cherry" ]
>>> for i,j in enumerate(fruits):
... print(i,j)
...
0 Apple
1 Berry
2 Cherry
>>>

It outputs both the index (i) and the value (j).

That was easy!

Enumerating a Tuple

Lets see how you can enumerate a tuple.

You can enumerate a Python tuple, which isn’t very different from iterating over a list.
The code below shows you how to iterate over a tuple:

1
2
3
4
5
6
7
8
>>> fruits = [(15,"Fifteen"), (12,"Twelve"), (19,"Nineteen")]
>>> for i,j in enumerate(fruits):
... print(i,j)
...
0 (15, 'Fifteen')
1 (12, 'Twelve')
2 (19, 'Nineteen')
>>>

As expected, it outputs both the index and the value, where the value now is the whole tuple.

If you want instead a more clean output, you can use tuple unpacking.

With tuple unpacking (and f-strings formatting), you get a clean output like this:

1
2
3
4
5
6
7
8
>> fruits = [(15,"Apple"), (12,"Berry"), (19,"Cherry")]
>>> for i,(price,name) in enumerate(fruits):
... print(f"index {i}, price {price} and name {name}")
...
index 0, price 15 and name Apple
index 1, price 12 and name Berry
index 2, price 19 and name Cherry
>>>

Enumerating a String

Can you iterate over a string object?

Yes you can, every item in the string is a character. This gives you the character index and the character value, for every character in the string.

If you have a string you can iterate over it with enumerate(string).
The example below shows you how to do that:

1
2
3
4
5
6
7
8
9
10
>>> fruit = "Apple"
>>> for i,j in enumerate(fruit):
... print(i,j)
...
0 A
1 p
2 p
3 l
4 e
>>>

The code output above shows both the index and the value for every element of the string.

Enumerate with a Different Starting Index

The enumerate() function has another parameter, the starting index. By default indicides start at zero.

You can change that, lets say you want to start at the second parameter.

The code will look like this:

1
2
3
4
5
6
7
8
9
10
>>> fruit = "Apple"
>>> for i,j in enumerate(fruit, start=2):
... print(i,j)
...
2 A
3 p
4 p
5 l
6 e
>>>

Enumerate Dictonaries

It makes no sense to enumerate on a dictionary, because a dictionary is not a sequence.
A dictionary does not have an index, it’s not always in the same order.

If you want to iterate over a dictionary, you don’t need enumerate but the traditional:

1
2
3
4
5
6
7
8
>>> d = {'a':1, 'b':2, 'c':3}
>>> for k,v in d.items():
... print(k,v)
...
a 1
b 2
c 3
>>>

If you are a beginner, then I highly recommend this book.

Exercise

Try the exercises below

  1. Combine a for loop with an enumerable.

Download examples