Python - Print dictionary of list values
Last Updated :
11 Dec, 2023
In this article, we will explore various ways on How to Print Dictionary in Python of list values. A dictionary of list values means a dictionary contains values as a list of dictionaries in Python.
Example:
{'key1': [{'key1': value,......,'key n': value}........{'key1': value,......,'key n': value}}],
------------------------
------------------------
'keyn': [{'key1': value,......,'key n': value}........{'key1': value,......,'key n': value}}]}
So we have to get the dictionaries present in the list according to the key. We can get this by using dict.items().
Print Dictionary in Python
Syntax :
d.items()
There are various ways to Print a Dictionary in Python. Here we are discussing some generally used methods for print dictionary values in python those are following.
- Using Single For loop
- Using Two For loop
- Using
pprint
Module - Using
json.dumps
- Using list comprehension and
join
Create a Python Dictionaries
In this example the below code defines a dictionary (`data`) where student names serve as keys, and each key corresponds to a list of dictionaries representing subject details and marks for that student.
Python3
# create a dictionary
# with student names as key
# values as subject details
data = {'manoja': [{'subject1': "java", 'marks': 98},
{'subject2': "PHP", 'marks': 89}],
'manoj': [{'subject1': "java", 'marks': 78},
{'subject2': "PHP", 'marks': 79}],
'ramya': [{'subject1': "html", 'marks': 78}]}
print(data)
Output :
manoja : {'subject1': 'java', 'marks': 98}
manoja : {'subject2': 'PHP', 'marks': 89}
manoj : {'subject1': 'java', 'marks': 78}
manoj : {'subject2': 'PHP', 'marks': 79}
ramya : {'subject1': 'html', 'marks': 78}
Print a Dictionary Value in Python using Single For loop
Use a single for loop to iterate through the dictionary items. Print each key-value pair using the loop variable
Example : In this example the below code iterates through key-value pairs in the dictionary `my_dict` using a loop and prints each pair in the format "key: value".
Python3
#Using only loop
for key, values in my_dict.items():
print(f"{key}: {values}")
Output :
manoja : {'subject1': 'java', 'marks': 98}
manoja : {'subject2': 'PHP', 'marks': 89}
manoj : {'subject1': 'java', 'marks': 78}
manoj : {'subject2': 'PHP', 'marks': 79}
ramya : {'subject1': 'html', 'marks': 78}
Print a Dictionary Value in Python using Two For loop
To print the values of dictionary in Python using a simple loop, you can iterate over the values of the dictionary and print each value.
Example : In this example the below Python code iterates through a dictionary (`data`), printing each key along with its corresponding values in a nested loop.
Python3
# get the list of data
# using items() method
for key, values in data.items():
for i in values:
print(key, " : ", i)
Output:
manoja : {'subject1': 'java', 'marks': 98}
manoja : {'subject2': 'PHP', 'marks': 89}
manoj : {'subject1': 'java', 'marks': 78}
manoj : {'subject2': 'PHP', 'marks': 79}
ramya : {'subject1': 'html', 'marks': 78}
Print Dictionary Value in Python using pprint Module
In Python, utilize the `pprint` module to neatly print dictionary values. Import the module with `from pprint import pprint` and then use `pprint(dictionary)` for a clear and formatted display, enhancing readability, especially for intricate data structures.
Example : In this example the below code incorrectly uses 'my_dict' instead of 'data' in the `pprint` function call for pretty-printing a dictionary representing student details with subject marks.
Python3
#import the library
import from pprint import print
#print the dictionary
pprint(my_dict)
Output :
manoja : {'subject1': 'java', 'marks': 98}
manoja : {'subject2': 'PHP', 'marks': 89}
manoj : {'subject1': 'java', 'marks': 78}
manoj : {'subject2': 'PHP', 'marks': 79}
ramya : {'subject1': 'html', 'marks': 78}
Print all the values of a Dictionary in Python using json.dumps
I
n Python, import the json module and use `
json.dumps
(dictionary)` to quickly convert a dictionary into a formatted JSON string for easy printing or serialization.
Example :
In this example the below code forms a student subject details dictionary 'data' and prints its JSON format with 4-space indentation using 'json.dumps(data, indent=4)'.
Python3
#import json library
import json
#print the dictionary
print(json.dumps(my_dict, indent=4))
Output :
manoja : {'subject1': 'java', 'marks': 98}
manoja : {'subject2': 'PHP', 'marks': 89}
manoj : {'subject1': 'java', 'marks': 78}
manoj : {'subject2': 'PHP', 'marks': 79}
ramya : {'subject1': 'html', 'marks': 78}
Printing a Python Dictionary value using List Comprehension and Join
This method employs list comprehension to iterate over a dictionary's key-value pairs, then uses the `join` method to concatenate and print them in a formatted way.
Example : In this example the below code prints each key-value pair from the dictionary `my_dict` with lists as values on separate lines.
Python3
print('\n'.join([f"{key}: {values}" for key, values in my_dict.items()]))
Output :
manoja : {'subject1': 'java', 'marks': 98}
manoja : {'subject2': 'PHP', 'marks': 89}
manoj : {'subject1': 'java', 'marks': 78}
manoj : {'subject2': 'PHP', 'marks': 79}
ramya : {'subject1': 'html', 'marks': 78}
Similar Reads
Python Print Dictionary Keys and Values When working with dictionaries, it's essential to be able to print their keys and values for better understanding and debugging. In this article, we'll explore different methods to Print Dictionary Keys and Values.Example: Using print() MethodPythonmy_dict = {'a': 1, 'b': 2, 'c': 3} print("Keys:", l
2 min read
Get List of Values From Dictionary - Python We are given a dictionary and our task is to extract all the values from it and store them in a list. For example, if the dictionary is d = {'a': 1, 'b': 2, 'c': 3}, then the output would be [1, 2, 3].Using dict.values()We can use dict.values() along with the list() function to get the list. Here, t
2 min read
Inverse Dictionary Values List - Python We are given a dictionary and the task is to create a new dictionary where each element of the value lists becomes a key and the original keys are grouped as lists of values for these new keys.For example: dict = {1: [2, 3], 2: [3], 3: [1]} then output will be {2: [1], 3: [1, 2], 1: [3]}Using defaul
2 min read
Python Convert Dictionary to List of Values Python has different types of built-in data structures to manage your data. A list is a collection of ordered items, whereas a dictionary is a key-value pair data. Both of them are unique in their own way. In this article, the dictionary is converted into a list of values in Python using various con
3 min read
Get Python Dictionary Values as List - Python We are given a dictionary where the values are lists and our task is to retrieve all the values as a single flattened list. For example, given the dictionary: d = {"a": [1, 2], "b": [3, 4], "c": [5]} the expected output is: [1, 2, 3, 4, 5]Using itertools.chain()itertools.chain() function efficiently
2 min read