Open In App

Python – Print dictionary of list values

Last Updated : 11 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

In 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}



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads