Answer: Iterating over dictionaries in Python is a crucial topic, there are various methods for iterating the dictionaries, but using “for loop” is the most common method.
Table of Contents:
A dictionary in Python is a built-in data type to store and manage data in pairs of keys and values. In this article, let’s learn about iterating over dictionaries using ‘for-loop’ in Python in detail. It is important to iterate because it provides an efficient way to interact with the key-value pairs in a dictionary, which allows you to perform operations on them.
Methods to Iterate over Dictionaries using ‘for-loop’ in Python
Python provides various built-in methods to iterate over dictionaries using a ‘for-loop’. Following are some of these methods explained with the help of examples:
Method 1: Iterating over Keys in a Dictionary in Python
This is the default method for iterating over dictionaries, Here’s how to access each key in the dictionary.
Example:
Output:
In this case, the key will take on the values ‘a’, ‘b’, and ‘c’.
Method 2: Iterating over Values in a Dictionary in Python
In this method, if we want to iterate through the values in dictionaries we can use the method called “.values”.
Example:
Output:
In this case, the values will take on the values ‘hi’, ‘from’, and ‘intellipaat’.
Method 3: Iterating over Both Values and Keys in a Dictionary in Python
In this method, we can iterate both values and keys in a dictionary using “.items()”. This method is the most efficient way to return an iterable object that provides each item as a tuple(key, value). By using a for-loop we can return the tuple directly.
Example:
Output:

In the loop, the key holds the dictionary key and the value holds the dictionary value, this method is the most easiest and readable method for accessing both keys and values.
Method 4: Filtering During Iteration in Dictionaries in Python
This method is an efficient way to use the conditional loop statements only when the required condition is met.
Example:
It prints the keys and values only if they meet a certain condition else it does not print.
Output:
Method 5: Iterating over a Nested Dictionary in Python
Dictionaries that are stored as values within another dictionary are called “nested dictionaries”. Here the usage of “for-loop” is to iterate over each key-value pair in the dictionary by checking if the value is in another dictionary and recursively calling the function to handle nested dictionaries.
Example:
Output:
Here in nested dictionaries, we use two for loops one is for the outer dictionary and one is for the inner dictionary. isinstance() is to check if a value is in a dictionary. This method allows for handling nested dictionaries.
Method 6: Iterating over Index and Values in a Dictionary in Python
Using the enumerate() function in Python is typically used to get both the index and the value at each iteration in a dictionary.
Example:
Output:
This code uses the enumerate() function to iterate over a dictionary, printing both indexes and (key, value) pairs
Conclusion
Iterating over dictionaries in Python can be done in many ways but the most effective method is using “for-loop”. Other ways include keys, values, or both by using .items() for filtering the loop must meet the required conditions, nested dictionaries also can be iterated recursively using instance() method, for getting both indexes and values we use enumerate() function, these methods allow efficient and readable codes for handling the dictionaries.