How to print all the values of a dictionary in Python?


Python has a built-in method called values() that returns a view object. The dictionary's values are listed in the view object.

We can use the values() method in Python to retrieve all values from a dictionary. Python's built-in values() method returns a view object that represents a list of dictionaries containing every value.

This article explains about the various ways to print all the values of a dictionary in Python.

Using dict.values() Method

Python's dict.values() method can be used to retrieve the dictionary values, which can then be printed using the print() function.

Example

Following is an example to print all the values of a dictionary using dict.values() method −

dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } print(dictionary.values())

Output

Following is an output of the above code −

dict_values['Pride and Prejudice', '1813', 'Jane Austen', 'Elizabeth Bennet']

Using for Loop

The dict.values() function in the dictionary class of Python returns an iterable list of dictionary values. The series of values returned by the method values() can be iterated over using a for loop, and each value can be printed as we go.

Example

Following is an example to print all the values of a dictionary using for loop −

dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } # Iterating over all the values of a dictionary and printing them one by one for values in dictionary.values(): print(values)

Output

Following is an output of the above code −

Pride and Prejudice
1813
Jane AustenElizabeth Bennet

By creating a list of all values

The iterable sequence that the dict.values() function returns can likewise be used to generate a list of values. The list's entire contents can then be printed (all values of the dictionary).

Example

Following is an example to print all the values of a dictionary by creating a list of all values −

dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } # Getting all the values of a dictionary as a list list_of_the_values = list(dictionary.values()) # Printing the list which contains all the values of the dictionary print(list_of_the_values)

Output

Following is an output of the above code

['Pride and Prejudice', '1813', 'Jane Austen', 'Elizabeth Bennet']

By creating a list comprehension

We may loop through all of the dictionary's contents using this list comprehension and then display each value one at a time.

Example

Following is an example to print all the values of a dictionary by creating a list comprehension −

dictionary = { 'Novel': 'Pride and Prejudice', 'year': '1813', 'author': 'Jane Austen', 'character': 'Elizabeth Bennet' } # Iterating over all the values of the dictionary and printing them one by one [print(values) for values in dictionary.values()]

Output

Following is an output of the above code −

Pride and Prejudice
1813
Jane Austen
Elizabeth Bennet

Printing all values of a nested dictionary

Consider the case where we have nested dictionaries, a class of dictionaries that uses other dictionaries as value fields. We can use recursion to loop through each value in a nested dictionary.

We have developed a method that returns all values from the supplied dictionary. Iterating through all key-value pairs in the dictionary, it determines whether or not each pair's value is of the dictionary type,It yields the value if value is not a dict type.

When a value is of the dictionary type, this function will call itself in order to access each value in a nested dictionary and return them all. The procedure continues until all nested dictionaries have been covered.

Example

Following is an example to print all the values of a nested dictionary −

# Nested Dictionary Novels = { 'Novel 1': {'Name': 'Pride and Prejudice', 'year': 1813, 'author': 'Jane Austen'}, 'Novel 2': {'Name': 'Listen to Your Heart: The London Adventure', 'year': 2022, 'author': 'Ruskin Bond'}, 'Novel 3': {'Name': 'A Place Called Home', 'year': 2021, 'author': 'Preeti Shenoy'}, 'Novel 4': {'Name': 'Leaders, Politicians, Citizens', 'year': 2020, 'author': ' Rasheed Kidwai '}, } def all_the_values(dictionary): # Iterating over all the values of the dictionary for keys , values in dictionary.items() # If the values are of dictionary type then yield all the values in the nested dictionary if isinstance(values, dict): for x in all_the_values(values): yield x else: yield values # Iterating over all the values of a nested dictionary and printing them one by one. for values in all_the_values(Novels): print(values)

Output

Following is an output of the above code.

Pride and Prejudice
1813
Jane Austen
Listen to Your Heart: The London Adventure
2022
Ruskin Bond
A Place Called Home
2021
Preeti Shenoy
Leaders, Politicians, Citizens
2020
Rasheed Kidwai

Updated on: 23-Aug-2023

85K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements