How to Iterate Over Dictionaries Using ‘For’ Loops in Python

A dictionary in Python is a flexible data structure that can store the data of multiple data types. The data exists as key-value pairs. A key-value pair contains a set of data associated with keys representing something and the pairs containing the specific details regarding these key identifiers. The dictionaries can be used for loops in Python in several ways.

This article discusses various methods of iterating over the dictionaries using the for loops and getting the key-value pairs from it.

How to Iterate Over Dictionaries Using ‘For’ Loops in Python?

Dictionaries store the information inside it in the form of a pair which is called a key value pair. Dictionaries are repeatable objects in Python and they can be iterated through several methods. The most convenient way to iterate through a dictionary is by using the for loop that enables you to access each value in the dictionary separately.

The following flow chart shows how keys and their respective values are stored in a Python dictionary:

Iterate Over Dictionaries Using 'For' Loops in Python 1

Let us discuss each method of iterating over a dictionary Using ‘For’ loops in Python in detail.

Example 1: Iterating Over Dictionaries Using ‘For’ Loop to Check for Individual Key

In Python, the for loop can be iterated over the given dictionary. In the following example, the for loop is used to iterate over the “automotive” dictionary which checks for each individual key and prints the keys on separate lines:

automotive = {
"Brand": "Chevrolet",
"Year": "1911",
"Headquarters": "Detroit, Michigan, USA",
}
print('The keys are:')
for key in automotive:
print(key)

In the above code,

  • A Python dictionary named “automotive” is defined with keys representing the information of the automotive.
  • A for-loop runs through each key in the automotive dictionary
  • print(key) prints each key of the automotive dictionary individually.

Output

The following output shows that each key is printed on a separate line using a for-loop iterator:

Iterate Over Dictionaries Using 'For' Loops in Python a

Example 2: Iterating Over Dictionaries Using ‘For’ Loops and values() Method

A built-in Python method values() is used to extract the values from the key-value pairs. The following code explains how values can be extracted using the values() method in Python:

automotive = {
"Brand": "Chevrolet",
"Year": "1911",
"Headquarters": "Detroit, Michigan, USA",
}
print('The values are:')
for value in automotive.values():
print(value)

In the above code,

  • The automotive dictionary with the key-value pairs is defined.
  • The for loop runs through the automotive dictionary to check each value..
  • print(value) prints out all the values from the key-value pairs in individual lines.

Output

The following output shows that values containing the specific details for the keys are printed on separate lines using the values() method:

Iterate Over Dictionaries Using 'For' Loops in Python 2

Example 3: Iterate Over Dictionaries Using ‘For’ Loops and items() Method

The Python function items() extract all the keys and values from the automotive dictionary. The following code uses items() to extract the key value pairs and prints them by separating them with a colon:

automotive = {
"Brand": "Chevrolet",
"Year": "1911",
"Headquarters": "Detroit, Michigan, USA",
}
print('The keys and values are:')
for key,value in automotive.items():
print(key, ":", value)

In the above code,

  • The automotive dictionary with similar key-value pairs as compared to the previous case is defined.
  • The key and values both are extracted as they are iterated through each key-value pair in the automotive dictionary.
  • The result printed represents the keys and the respective data they hold.

Output

The following output shows items() can be used to display both keys and values in a Python dictionary: 

Iterate Over Dictionaries Using 'For' Loops in Python 3

Example 4: Extracting the key-value Pairs Without Using any Inherent Python Method

The key-value pairs can be extracted without using any inherent Python method. Each iteration will give back the key from the dictionary. The value of the key will act as an index value that will retrieve the values from the dictionary. The following code explains how the key-value pairs can be extracted without using any inherent Python method.

automotive = {
"Brand": "Chevrolet",
"Year": "1911",
"Headquarters": "Detroit, Michigan, USA",
}
print('The keys and values are:')
for key in automotive:
print(key, ":", automotive[key])

In the above code,

  • The automotive dictionary is defined with the same key-value pairs.
  • The for loop is iterated for each value of the key in the automotive loop.
  • The print function prints the key-value pairs with a colon in between them. automotive[key] gets the corresponding information from their respective keys.

Output

The following output shows the key-value pairs from the automotive dictionary:

Iterate Over Dictionaries Using 'For' Loops in Python 4

Bonus Tip 1: Iterating Over Dictionaries Using keys() Method

The keys() method in Python prints all the keys in the dictionary. The following code explains how keys() can be used to retrieve all the keys present in the automotive dictionary:

automotive = {
"Brand": "Chevrolet",
"Year": "1911",
"Headquarters": "Detroit, Michigan, USA",
}
print(automotive.keys())

In the above code,

  • A Python dictionary named “automotive” is defined with keys representing the information to be provided about the vehicle. 
  • print(automotive.keys) will print the keys from the automotive dictionary.

Output

The following output shows the keys from the automotive dictionary using the keys() method:

Iterate Over Dictionaries Using 'For' Loops in Python 6

Bonus Tip 2: Iterating Over Dictionaries Using items() to Print Key-Value Pairs

All the key-value pairs are printed in the form of pairs and contained inside parentheses. The following code explains how items() can be used to print the key value elements in the form of pairs:

automotive = {
"Brand": "Chevrolet",
"Year": "1911",
"Headquarters": "Detroit, Michigan, USA",
}
print('The keys and values are:')
pairs = automotive.items()
print(pairs)

In the above code,

  • The automotive dictionary is defined with the same key-value pairs as before.
  • The Python method items() stores all the key-value pairs in a dictionary and stores them in the variable called pairs.
  • The pairs variable is printed which displays all the key-value pairs of the automotive dictionary.

Output

The following output shows the dict_items objects representing the key-value pairs in the parenthesis:

Iterate Over Dictionaries Using 'For' Loops in Python 7

Bonus Tip 3: Iterating Over Dictionaries Using Unpacking of the Dictionary

The key-value pairs are extracted using the * operator that unpacks the dictionary object. The data inside the dict object is accessed through the * method. The following code shows how the *dict method can be used to retrieve all the keys and values from the automotive dictionary:

automotive = {
"Brand": "Chevrolet",
"Year": "1911",
"Headquarters": "Detroit, Michigan, USA",
}
keys = [*automotive]
values = '{Brand}-{Year}-{Headquarters}'.format(*automotive, **automotive)
print("The keys are:", keys)
print("The values are:", values)

In the above code,

  • The automotive dictionary is defined.
  • keys=[*automotive] get all the keys from the key-value pairs and store them in the keys variable.
  • The values variable stores the formatted string. *automotive unpacks the keys as the positional argument while **automotive gets the key-value pairs as the keyword argument.

Output

The following output shows the list of keys and the formatted string representing the values from the automotive dictionary:

Iterate Over Dictionaries Using 'For' Loops in Python 9

Conclusion

Various Python functions like items(), keys(), and values() are used to iterate over the dictionaries using for loops in Python. Dictionaries enable efficient data storage and thus save a lot of time. It stores the key-value pairs which helps in data manipulation and its analysis. Following the methods covered in the article, the key-value pairs can be conveniently retrieved. This article discusses how the dictionaries can be iterated using “For” loops in Python.