
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print All Values of a Dictionary in Python
Python has a built-in method called values() that returns a view object. The dictionary 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.
In this article, we are going to see about the various ways to print all the values of the given dictionary in Python.
Using dict.values() Method
Python's dict.values() method can be used to retrieve the dictionary values which can 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())
Following is the output of the above program ?
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)
Below is an output of the above program ?
Pride and Prejudice 1813 Jane Austen Elizabeth Bennet
By creating a list of all values
In Python, dictionaries are used to store data in key-value pairs. By creating a list of all values, we can extract just the data portion for further processing, analysis or display.
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)
Below is an output of the above program ?
['Pride and Prejudice', '1813', 'Jane Austen', 'Elizabeth Bennet']
By creating a list comprehension
In this method we will perform loop through all of the dictionary's contents using this list comprehension and then display each value one at a time.
Example
Here 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
Below is an output of the above program ?
Pride and Prejudice 1813 Jane Austen Elizabeth Bennet
Printing all values of a nested dictionary
Let's consider a case in which, 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.
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)
Below is an output of the above example ?
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