How to Print All the Values of a Dictionary in Python?Introduction:Here we are learning to print all the values of a dictionary. Dictionary of the list is defined as a dictionary containing values from the list of dictionaries in Python. Python has a method called values() that returns a view object. Dictionary results are listed in the object view. We can use the values() method in Python to store all the values in a dictionary. Python's built-in values() method returns an object view that represents a list of dictionaries containing each value. Syntax: The syntax is given below - There are many ways to print all the values of a dictionary in Python. Here we discuss some ways to print dictionary values in Python as shown below - 1. Using the dict.values() Method:The dict.values() method in Python retrieves the dictionary values. Then it can be printed by using the print() function. Program Code: Here we give a program code to print all the values of a dictionary by using the dict.values() method in Python. The code is given below - Output: Now, we run the above code and print the values of the dictionary. The output is given below - The values of the dictionary are: dict_values(['Priyanka Adhikary', '2000', 'Female', 'JavaTpoint']) 2. Using the for-Loop:Using a loop is another way to copy all the results of a dictionary in Python. The dict.values() function returns a list of dictionary values. You can use a loop to iterate through the values returned by the value() method. Lastly, print each value each time. Program Code: Here we give a program code to print all the values of a dictionary by using the for loop in Python. The code is given below - Output: Now, we run the above code and print the values of the dictionary. The output is given below - 3. Creating the List of all Values:Creating a list of all values is another way to print all the values of a dictionary. The array returned by the dict.values() function. This function can also be used to create a list of values. The entire content of the list (all given values of the dictionary) can be printed. Program Code: Here, we give a program code to print all the values of the dictionary by creating a list of all values in Python. The code is given below - Output: Now, we run the above code and print the values of the dictionary. The output is given below - The values of the dictionary are: ['Priyanka Adhikary', '2000', 'Female', 'JavaTpoint'] 4. Creating the List Comprehension:Creating the list comprehension is another way to print all the values of a dictionary in Python. We can use this list comprehension to understand that it loops through the entire contents of the dictionary and then displays each value of the dictionary. Program Code: Here we give a program code to print all the values of a dictionary by creating the list comprehension in Python. The code is given below - Output: Now, we run the above code and print the values of the dictionary. The output is given below - Priyanka Adhikary 2000 Female JavaTpoint 5. Using the pprint Module:Using the pprint module is another way to print all the values of a dictionary. To properly print the dictionary in Python, use the "pprint" module. Use the "from pprint import pprint" option to import the module, and then use the "pprint (dictionary)" option to clean and format it to improve readability, especially in complex documents. Program Code: Here we give a program code to print all the values of a dictionary by using the pprint module in Python. The code is given below - Output: Now, we run the above code and print the values of the dictionary. The output is given below - dict_values(['Priyanka Adhikary', '2000', 'Female', 'JavaTpoint']) 6. Using the json.dumps:Using the json.dumps is another way to print all the values of a dictionary in Python. Program Code: Here, we give a program code to print all the values of a dictionary by using the json.dumps in Python. The code is given below - Output: Now, we run the above code and print the values of the dictionary. The output is given below - { "Name": "Priyanka Adhikary", "year": "2000", "Gender": "Female", "Website": "JavaTpoint" } 7. Print All the Values of a Nested Dictionary:Consider the case where dictionaries are nested, which is the class of dictionaries that use other dictionaries as field values. We can use recursion to loop through each value in the nested dictionary. We created a method that returns all values in the given dictionary. Iterate by all the key-value pairs in the dictionary and determine whether the value of each key-value is of the dictionary type. If the value is not of dictionary type, create this value. When the value is a dictionary, the function calls itself to access all the values in the nested dictionary and return them all. This process continues until all nested dictionaries are covered. Program Code: Here, we give a program code to print all the values of a nested dictionary in Python. The code is given below - Output: Now, we run the above code and print the values of the dictionary. The output is given below - Conclusion:Here we are learning to print all the values of a dictionary. We have learned various methods to print all the values of a dictionary. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India