Python: Iterate over dictionary with list values

In this article, we will discuss different ways to iterate over a dictionary with multiple values (list as value).

Table of Contents

Suppose we have a dictionary with list as values for most of the keys. It means multiple values are associated with some keys,

# Create a dictionary where multiple values are
# associated with a key
word_freq = {'is': [1, 3, 4, 8, 10],
             'at': [3, 10, 15, 7, 9],
             'test': 33,
             'this': [2, 3, 5, 6, 11],
             'why': [10, 3, 9, 8, 12]}

Now we want to iterate over all the values. There are different ways to do this. Let’s look at them one by one.

Iterate over a dictionary with list values using nested for loop

First, we will iterate over all the items (key-value pairs) of dictionary by applying a for loop over the sequence returned by items() function. As value field of a key-value pair can be a list, so we will check the type of value for each pair. If value is list then iterate over all its items using an another for loop and print it, otherwise just print the value. For example,

# Create a dictionary where multiple values are
# associated with a key
word_freq = {'is': [1, 3, 4, 8, 10],
             'at': [3, 10, 15, 7, 9],
             'test': 33,
             'this': [2, 3, 5, 6, 11],
             'why': [10, 3, 9, 8, 12]}

# Iterate over a dictionary with list values using nested for loop
for key, values in word_freq.items():
    print('Key :: ', key)
    if(isinstance(values, list)):
        for value in values:
            print(value)
    else:
        print(value)

Output:

Key ::  is
1
3
4
8
10
Key ::  at
3
10
15
7
9
Key ::  test
9
Key ::  this
2
3
5
6
11
Key ::  why
10
3
9
8
12

We iterated over all the key value pairs of dictionary and if any value field from the pair is a list object, then we printed each item of that list too. In our case, except one key, all other keys had the list values. Therefore, for each pair, we first checked if the type of value is list or not. If it is list, then only iterated over all the values from list, otherwise printed the value only.

Iterate over a dictionary with list values using list comprehension

In the previous example, we iterated over all list values for each key. But if you want to have a complete list of pairs then we can use the list comprehension too. As a key can have multiple values in our dictionary, so for each pair we will generate a list of pairs where key will remain same but value will be different in each pair. For example,

# Create a dictionary where multiple values are
# associated with a key
word_freq = {'is': [1, 3, 4, 8, 10],
             'at': [3, 10, 15, 7, 9],
             'test': [43, 22, 19 ],
             'this': [2, 3, 5, 6, 11],
             'why': [10, 3, 9, 8, 12]}

# Iterate over a dictionary with list values and create
# a pair of all key-value pairs.
pairs = [   (key, value) 
            for key, values in word_freq.items() 
            for value in values ]

for pair in pairs:
    print(pair)

Output:

('is', 1)
('is', 3)
('is', 4)
('is', 8)
('is', 10)
('at', 3)
('at', 10)
('at', 15)
('at', 7)
('at', 9)
('test', 43)
('test', 22)
('test', 19)
('this', 2)
('this', 3)
('this', 5)
('this', 6)
('this', 11)
('why', 10)
('why', 3)
('why', 9)
('why', 8)
('why', 12)

We iterated over all key value pairs using list comprehension, then for each value (list object), we iterated over all the items in that list too and created a list of key-value pairs.

Summary:

We learned different ways to iterate over a dictionary with list as values.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top