How to Iterate Over a Dictionary in Python?

The dictionary is an important Python data structure enabling users to store data in key-value pairs. However, manipulating dictionaries can be challenging, especially when dealing with large amounts of data. 

To iterate over a dictionary, various methods, such as keys(), items(), values(), etc., are used in Python. This post will discuss these ways and iterate over a dictionary. Let’s start with the following methods:

  • Method 1: Using keys() Method
  • Method 2: Using values() Method
  • Method 3: Using items() Function
  • Method 4: Using Dictionary Comprehension
  • Method 5: Using Index Position

Method 1: Using keys() Method

The “keys()” method in Python is a built-in function that is used to retrieve a list of all the keys in a dictionary. The for loop is used with the “keys()” function to iterate over a dictionary:

Code:

dict_value = {'Name': 'Joseph','Age': 22,'Height': 5.7, 'Salary': '$1000'}
for i in dict_value.keys():
    print(i)
  • The dictionary named “dict_value” is initialized.
  • The “dict_value.keys()” function is used to get the dictionary’s keys.
  • The for loop iterates through the dictionary’s keys and returns the output.

Output:

The key value of the dictionary has been iterated.

Method 2: Using values() Method

Python’s “values()” method retrieves a list of all dictionary values using the built-in method. This function is also used with the for loop to iterate over a dictionary. Here is an example code:

Code:

dict_value = {'Name': 'Joseph','Age': 22,'Height': 5.7, 'Salary': '$1000'}
for i in dict_value.values():
    print(i)
  • The “dict_value.values()” function is used to return the values of the dictionary keys.
  • The for loop iterates over the dictionary values returned by the “dict_value.values()” and displays the output.

Output:

The dictionary values have been iterated successfully.

Method 3: Using items() Function

Python’s “items()” function returns a view object that contains a list of (key, value) pairs from a dictionary. The below code uses the items() function along with the for loop to iterate over a dictionary:

Code:

dict_value = {'Name': 'Joseph','Age': 22,'Height': 5.7, 'Salary': '$1000'}
for i, x in dict_value.items():
    print(i,"->", x)
  • The “dict_value.items()” is used to get the element value of the given dictionary.
  • The for loop iterates over the items/elements of the dictionary and returns the dictionary keys and their values.

Output:

The items in the dictionary have been returned.

Method 4: Using Dictionary Comprehension

Dictionary comprehension creates a new dictionary by iterating over a list or another dictionary. Here is an example code to iterate over a dictionary:

Code:

dict_value = {'Name': 'Joseph','Age': 22,'Height': 5.7, 'Salary': '$1000'}
new_dict = {key: value for (key, value) in dict_value.items()}
print(new_dict)
  • The dictionary comprehension is used to create a new dictionary that has the same key-value pairs.
  • The “dict.items()” function is used to Iterate over the key-value pairs of a given dictionary and create a new key-value pair for each pair in the original dictionary.

Output:

The items in the dictionary have been returned as a dictionary after iteration.

Method 5: Using Index Position

In the below code, the for loop is used to iterate over the dictionary and get the element’s value of the dictionary with the help of the index:

Code:

dict_value = {'Name': 'Joseph','Age': 22,'Height': 5.7, 'Salary': '$1000'}
for index in dict_value:
  print(index, dict_value[index])

The for loop is used to iterate over the dictionary for each index and get the dictionary keys with their values.

Output:

The dictionary keys with their values have been returned successfully.

Conclusion

The keys() method, values() method, items() function, dictionary comprehension, and index position methods are used to iterate over a dictionary in Python. The keys(), values(), and items() functions are used along with a for loop to iterate over a dictionary in Python. The dictionary comprehension and other remaining methods can also iterate over a dictionary in Python. This Python guide presented various ways to iterate over the dictionary using numerous examples.