In this article we will explore how to use the Python enumerate() function.
Table of Contents
- Introduction
- Using enumerate() with lists
- Using enumerate() with strings
- Using enumerate() with dictionaries
- Using enumerate() with zip()
- Conclusion
Introduction
Python enumerate() function is a built-in function that allows to loop over Python iterables while keeping track of indexes of them.
The syntax of the Python enumerate() function is:
enumerate(iterable, start=0)
where:
- iterable – can be any iterable Python object like string, tuple, list, set, dictionary, and others.
- start – parameter to specify the starting index (optional). Defaults to 0.
Using enumerate() with lists
As a simple example we will create a Python list and then use a for loop with enumerate() function to print each element of the list along with the index of each element:
my_list = ['Apple', 'Banana', 'Orange', 'Pineapple']
for i, elem in enumerate(my_list):
print(i, elem)
and you should get:
0 Apple
1 Banana
2 Orange
3 Pineapple
Using enumerate() with strings
The enumerate() function with also work with Python strings, since they are iterable objects.
It will work similarly to the example with Python lists. The difference is that in case of strings we will be iterating over each character in a string and printing it out along with its index (position) in a string:
my_string = 'Apple'
for i, char in enumerate(my_string):
print(i, char)
and you should get:
0 A
1 p
2 p
3 l
4 e
Using enumerate() with dictionaries
Another interesting example is using enumerate() function with Python dictionaries.
While key-value pairs in the dictionary are not ordered and not indexed, using enumerate() can be a very useful option for your code.
The main difference in the output is that when iterating over a dictionary, you have the option of iterating over dictionary keys, dictionary values, or dictionary key-value pairs.
Iterating over dictionary keys
my_dict = {
'Apple': 3,
'Banana': 1,
'Orange': 2,
'Pineapple': 5
}
for i, key in enumerate(my_dict.keys()):
print(i, key)
and you should get:
0 Apple
1 Banana
2 Orange
3 Pineapple
Iterating over dictionary values
my_dict = {
'Apple': 3,
'Banana': 1,
'Orange': 2,
'Pineapple': 5
}
for i, value in enumerate(my_dict.values()):
print(i, value)
and you should get:
0 3
1 1
2 2
3 5
Iterating over dictionary key-value pairs
When iterating over key-value pairs combined with enumerate() functionality, the output you will get is each index and a tuple containing the key-value pair at each entry in the dictionary:
my_dict = {
'Apple': 3,
'Banana': 1,
'Orange': 2,
'Pineapple': 5
}
for i, (key, value) in enumerate(my_dict.items()):
print(i, (key, value))
and you should get:
0 ('Apple', 3)
1 ('Banana', 1)
2 ('Orange', 2)
3 ('Pineapple', 5)
Using enumerate() with zip()
A slightly more advanced example is using enumerate() with another Python function, for example zip().
The functionality of the two functions combined allows to iterate over multiple lists at the same time while keeping track of indexes of pairs of elements:
fruits = ['Apple', 'Banana', 'Orange', 'Pineapple']
prices = [3, 1, 2, 5]
for i, (fruit, price) in enumerate(zip(fruits, prices)):
print(i, fruit, price)
and you should get:
0 Apple 3
1 Banana 1
2 Orange 2
3 Pineapple 5
Conclusion
In this article we explored the Python enumerate() function.
Now that you know the basic functionality, you can practice using it with other iterable data structures for more complex use cases.
Feel free to leave comments below if you have any questions or have suggestions for some edits and check out more of my Python Functions tutorials.