Loop / Iterate over all values of dictionary in Python

In this article, we will discuss different ways to iterate over all values of a python dictionary.

Table of Contents

Loop over all values of dictionary using values()

In python, dictionary class provides a function values(), it returns an iterable sequence of all values of the dictionary i.e. dict_values. It is a view of all values of the dictionary, it means any change in original dictionary will be reflected in this sequence. Also, we can not use indexing with this sequence. But we can use this along with a for loop to iterate over all values of the dictionary. For example,

# Dictionary of string and integers
word_freq = {
    'Hello' : 56,
    'at'    : 23,
    'test'  : 43,
    'This'  : 78,
    'Why'   : 11
}

# Iterate over all values of a dictionary
for value in word_freq.values():
    print(value)

Output:

56
23
43
78
11

We iterated over all values of dictionary and printed them line by line.

Advertisements

Loop over all values of dictionary using for loop

A dictionary object can also be used as an iterable object to iterate over the keys of dictionary. If we use it with a for loop then we can easily iterate over all keys of the dictionary. Then during iteration we can select the value associated with that key. For example,

# Dictionary of string and integers
word_freq = {
    'Hello' : 56,
    'at'    : 23,
    'test'  : 43,
    'This'  : 78,
    'Why'   : 11
}

# Iterate over all values of a dictionary
for key in word_freq:
    print(word_freq[key])

Output:

56
23
43
78
11

Here, we used the dictionary object as an iterator and iterated over all keys of the dictionary and selected value for each of the key and printed them one by one.

This is not an efficient solution because we are iterating over keys first and then selecting the value associated with it. Instead of this we could have directly iterated over all values of dictionary, like we did in the previous example. We included this example, to just tell that there is an another way to do the job, may be if you are interested in both key and value then this could be the solution for you.

Loop over all values of dictionary using items()

If you are more inclined toward the previous solution, but it looks confusing to you, because we are using the dictionary object as iterator. Then this solution is for you.

In python, dictionary class provides a function items(), it returns an iterable sequence of all key-value pairs of the dictionary i.e. dict_items. It is a view of all items (key-value pairs) of the dictionary, it means any change in original dictionary will be reflected in this sequence. Also, we can not use indexing with this sequence. We can use this along with a for loop to iterate over all pairs of the dictionary and while iteration we can select the second element of the pair / tuple i.e. is the value. For example,

# Dictionary of string and integers
word_freq = {
    'Hello' : 56,
    'at'    : 23,
    'test'  : 43,
    'This'  : 78,
    'Why'   : 11
}

print('--------')

# Iterate over all values of a dictionary
for key, value in word_freq.items():
    print(value)

Output:

56
23
43
78
11

We iterated over all values of dictionary and printed them line by line.

Summary:

We learned about different ways to iterate over all values of a dictionary in python.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

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