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:
The returned object can be treated like an iterator: the next method call will work:
1 | browsers = ['Chrome','Firefox','Opera','Vivaldi'] |
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 | "Apple","Berry","Cherry" ] fruits = [ |
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 | 15,"Fifteen"), (12,"Twelve"), (19,"Nineteen")] fruits = [( |
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 | >> fruits = [(15,"Apple"), (12,"Berry"), (19,"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 | "Apple" fruit = |
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 | "Apple" fruit = |
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 | 'a':1, 'b':2, 'c':3} d = { |
If you are a beginner, then I highly recommend this book.
Exercise
Try the exercises below
- Combine a for loop with an enumerable.