In this article we will discuss different ways to iterate over all key-value pairs of a dictionary.

Table of Contents:

Suppose we have a dictionary with string as key and integers as value i.e.

'''
Creating Dictionaries with string as key and int as value
'''                                  
word_freq = {   "Hello" : 7,
                "hi" : 10,
                "there" : 45,
                "at" : 23,
                "this" : 77 }

Now let’s see how to iterate over this dictionary using 4 different techniques i.e.

Iterate over a dictionary using for loop over keys

A dictionary object can also be used as an iterable obejct, to iterate over all keys of dictionary. So, we can easily apply for loop on a dictionary. By using for in dictionary, it loops through all the keys in dictionary. For each key we will select the value associated with it and print them.

word_freq = {   "Hello" : 7,
                "hi" : 10,
                "there" : 45,
                "at" : 23,
                "this" : 77 }

# Iterate over the dictionary using for loop
for key in word_freq:
    value = word_freq[key]
    print(key, " :: ", value)

Output:

Hello  ::  7
hi  ::  10
there  ::  45
at  ::  23
this  ::  77

Its not an efficient solution because we are iterating over all the keys in dictionary and for each key we are again searching for its associated value.

Let’s see an efficient method i.e.

Iterate over key-value pairs of dictionary using dict.items()

In Python, dictionary class provides a function items(), which returns an sequence of all key-value pairs of dictionary. This sequence is an iterable View object of all key,value elements in the dictionary. Its backed by original dictionary. Let’s use this to iterate over all key-value pairs of dictionary,

word_freq = {   "Hello" : 7,
                "hi" : 10,
                "there" : 45,
                "at" : 23,
                "this" : 77 }


# Iterate over all key-value pairs of dictionary
for key,value in word_freq.items():
    print(key, " :: ", value)

Output:

Hello  ::  7
hi  ::  10
there  ::  45
at  ::  23
this  ::  77

As, view object is backed by original dictionary, therefore any changes made in dictionary will be reflected in it.
For example,

Take a view object of dictionary i.e.

# Take a dictionary view 
view_obj =  word_freq.items()

print(view_obj)

Output

dict_items([('this', 77), ('hi', 10), ('Hello', 7), ('there', 45), ('at', 23)])

Now modify the dictionary

word_freq["hi"] = 90

print(view_obj)

Now same view object will also be modified because its backed by original dictionary

dict_items([('this', 77), ('hi', 90), ('Hello', 7), ('there', 45), ('at', 23)])

Read More,

 

Iterate over a dictionary using list comprehension

As dictionary’s items() function returns an iterable sequence of key-value pairs, so we can also use this list comprehension to iterate over all pairs of diction. For example,

word_freq = {   "Hello" : 7,
                "hi" : 10,
                "there" : 45,
                "at" : 23,
                "this" : 77 }


[   print(key, ' :: ', value)
    for key, value in word_freq.items() ]

Output:

Hello  ::  7
hi  ::  10
there  ::  45
at  ::  23
this  ::  77

Iterate over specific key-value pairs of dictionary

We can also iterate over specific key-value pairs of dictionary, it means the pairs which satisfy a certain condition. For example, loop our pairs of dictionary, where value is greater than 20,

word_freq = {   "Hello" : 7,
                "hi" : 10,
                "there" : 45,
                "at" : 23,
                "this" : 77 }


# Iterate over all key-value pairs of dictionary
for key,value in word_freq.items():
    if value > 20:
        print(key, " :: ", value)

Output:

there  ::  45
at  ::  23
this  ::  77

Summary:

We learned about four different ways to iterate over all key-value pairs of dictionary.