You can iterate a Python dictionary using the enumerate() function. which is used to iterate over an iterable object or sequence such as a list, tuple, string, set, or dictionary and return a tuple containing each element present in the sequence and their corresponding index.
In this article, I will explain enumerate() function and using its syntax, parameters, and usage how we can return the counter value of each key/value pair of the dictionary while iterating the dictionary using this function.
1. Quick Examples of Iterate Dictionary using enumerate()
Following are quick examples of iterate dictionary using enumerate() function.
# Below are the quick examples
# Initialize the dictionary
technology = {“course” : “Python”, “fee” : 4000, “duration” : “45 days”}
# Example 1: Iterate over all key-value pairs of the dictionary by index
# using enumerate()
print(“Iterate key/value pairs by index:”)
for i, (x, y) in enumerate(technology.items()):
print(i, x, “/”, y)
# example 2: Iterate over all keys of dictionary by index
# using enumerate()
print(“Iterate all keys by index:”)
for i, x in enumerate(technology.keys()):
print(i, “::”, x)
# Example 3: Iterate over all values of dictionary by index
# using enumerate()
print(“Iterate all values by index:”)
for i, y in enumerate(technology.values()):
print(i, “::”, y)
2. enumerate() Function
Enumerate() function is a built-in function provided by Python. It takes an iterable object and returns enumerate object. This function automatically returns the counter(index) of each value present in the iterable object like a list, tuple, or string. By default, the counter/index value starts from ‘0’. If we want to specify the starting counter we can use the start parameter as an optional parameter of enumerate() function.
2.1 Syntax of enumerate()
Following is a syntax of enumerate() function.
# Syntax of enumerate()
enumerate(iterable_object, start=0)
2.2 Parameters
It allows two parameters.
iterable_object: ( such as list, tuple, string, and dictionary) It supports iteration.
start : Using this we can specify starting point of counter, by default it is ‘0’.
2.3 Return Value
It returns enumerate object.
3. Iterate over all key-value pairs of dictionary by index
We know that dictionaries are unordered, there is no index to enumerate over. Instead, you can use the items() method of the dictionary to return a list of key-value pairs, and then apply enumerate() to the list of key/value pairs.
Let’s enumerate over the dictionary to get the counter(index) values of each key/value pair of the dictionary. Where the counter value starts from ‘0’.
# Initialize the dictionary
technology = {“course” : “Python”, “fee” : 4000, “duration” : “45 days”}
print(“Dictionary:”, technology)
# Iterate over all key-value pairs of the dictionary by index
# using enumerate()
print(“Iterate key/value pairs by index:”)
for i, (x, y) in enumerate(technology.items()):
print(i, x, “/”, y)
Yields below output.
4. Iterate over all keys of dictionary by index
The keys() function of the dictionary in Python is used to return an iterable sequence of all keys of the dictionary. We can pass it into the enumerate() function, which will return the keys along with the index position. For example,
# Iterate over all keys of dictionary by index
# using enumerate()
print(“Iterate all keys by index:”)
for i, x in enumerate(technology.keys()):
print(i, “::”, x)
Yields below output.
Using the above code, you can access the corresponding values of keys along with the index using dict[key].
# Iterate over all keys of dictionary by index
# using enumerate()
print(“Iterate all keys by index:”)
for i, x in enumerate(technology.keys()):
print(i, “::”, x, “/”, technology[x])
# Output:
# Iterate all keys by index:
# 0 :: course / Python
# 1 :: fee / 4000
# 2 :: duration / 45 days
5. Iterate over all values of dictionary by index
The values() function of the dictionary is used to return an iterable sequence of all values of the dictionary. We can pass it into the enumerate() function, it will return all values of the dictionary along with the index position. For example,
# Iterate over all values of dictionary by index
# using enumerate()
print(“Iterate all values by index:”)
for i, y in enumerate(technology.values()):
print(i, “::”, y)
# Output:
# Iterate all values by index:
# 0 :: Python
# 1 :: 4000
# 2 :: 45 days
6. Conclusion
In this article, I have explained the Python enumerate() function and how we can return the counter value of each key/value pair presented in the dictionary while iterating the dictionary using this function. And also explained using enumerate() how we can iterate all keys and values of the dictionary separately.
Happy learning!!
You can iterate a Python dictionary using the enumerate() function. which is used to iterate over an iterable object or sequence such as a list, tuple, string, set, or dictionary and return a tuple containing each element present in the sequence and their corresponding index. In this article, I will explain enumerate() function and using its syntax, Read More Python, Python Tutorial, Pending review by admin, Python Dictionary Examples