How to Iterate over Dictionaries using For Loop in Python?

How to Iterate over Dictionaries using For Loop in Python?

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:

Python

Output:

image 123

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: 

Python

Output:  

image 124

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:

Python

Output:

Iterating over Both Values and Keys in a Dictionary in Python 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.

Python

Output:

image 125

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:

Python

Output:

image 126

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: 

Python

Output

image 127

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.

 

The blogs below provide tutorials on managing files and importing modules in Python-

Rename DataFrame Columns – Explore how to rename columns in a pandas DataFrame with examples provided in this article.
Current Script Name – This article covers the method to get the current script name in Python, along with examples.
Read File Line by Line – Learn how to read files line by line in Python, demonstrated with practical examples.
Get Current Date and Time – Find out how to obtain the current date and time in Python through this example-based guide.
Delete File or Directory – This guide explains how to delete files or directories in Python, with illustrative examples.
Import Module from Different Directory – Understand how to import modules from different directories in Python using example code.
Get Row Count of DataFrame – Discover how to count rows in a DataFrame with Python, supported by clear examples.
String Lowercase – The process of converting strings to lowercase in Python is explained here with examples.
NumPy where – Gain insight into the use of NumPy’s where function with examples provided in this tutorial.

About the Author

Senior Consultant Analytics & Data Science, Eli Lilly and Company

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner