We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Python: Print all values of a dictionary

In this article, we will discuss different ways to print all values of dictionary. Then we will also cover the scenario, where we need to print all the values of a nested dictionary i.e. dictionary of dictionaries.

Print all values of a python dictionary using for loop

In Python, the dictionary class provides a function dict.values(), which returns an iterable sequence of dictionary values. Using for loop we can iterate over the sequence of values returned by the function values() and while iteration we can print each value. For example,

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

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

Output:

Advertisements

56
23
43
78
11

Print all values of a python dictionary by creating a list of all values

We can also create a list of values from the iterable sequence returned by dict.values() function. Then we can print all the items of the list (all values of the dictionary). For example,

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

# Get all values of a dictionary as list
list_of_values = list(word_freq.values())

# Print the list containing all values of dictionary
print(list_of_values)

Output:

[56, 23, 43, 78, 11]

Print all values of a python dictionary by creating a list comprehension

We can also use this list comprehension to iterate over all the values of dictionary and then print each value one by one. For example,

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

# Iterate over all values of dictionary 
# and print them one by one
[print(value) for value in word_freq.values()]

Output:

56
23
43
78
11

Print all values of a nested dictionary in Python – Dictionary of dictionaries

Suppose we have a nested dictionary i.e. a kind of dictionaries which has another dictionary objects as value fields. To iterate over all the values of a nested dictionary, we can use the recursion.

We have created a function, which will yield all values of the given dictionary. It will iterate over all key-value pairs in dictionary and for each pair, it checks if the value is of dictionary type or not,

  • If value is not of dict type, then it yields the value.
  • If value is of dictionary type, then this function will call itself to access all values of nested dictionary and yield them too, one by one. The process goes on and on till all the nested dictionaries are covered.

For example,

# A Nested dictionary i.e. dictionaty of dictionaries
students = {
            'ID 1':    {'Name': 'Shaun', 'Age': 35, 'City': 'Delhi'},
            'ID 2':    {'Name': 'Ritika', 'Age': 31, 'City': 'Mumbai'},
            'ID 3':    {'Name': 'Smriti', 'Age': 33, 'City': 'Sydney'},
            'ID 4':    {'Name': 'Jacob', 'Age': 23, 'City': {'perm': 'Tokyo', 'current': 'London'}},
            }


def all_values(dict_obj):
    ''' This function generates all values of
        a nested dictionary. 
    '''
    # Iterate over all values of the dictionary
    for key , value in dict_obj.items():
        # If value is of dictionary type then yield all values
        # in that nested dictionary
        if isinstance(value, dict):
            for v in all_values(value):
                yield v
        else:
            yield value

# Iterate over all values of a nested dictionary 
# and print them one by one.
for value in all_values(students):
    print(value)

Output:

Shaun
35
Delhi
Ritika
31
Mumbai
Smriti
33
Sydney
Jacob
23
Tokyo
London

We iterated over all values of a nested dictionary and printed them one by one. If you want to get all values of a nested dictionary as a list, then you can just put the values yeilded by the the function all_values() to a list. For example,

# get all values of a nested dictionary
values = list(all_values(students) )

# Print the list containing all values of the nested dictionary 
print(values)

Output:

['Shaun', 35, 'Delhi', 'Ritika', 31, 'Mumbai', 'Smriti', 33, 'Sydney', 'Jacob', 23, 'Tokyo', 'London']

Summary:

We learned ways to print all values of a dictionary, including the nested dictionaries.

Cart

Your Cart is Empty

Back To Shop