Skip to content
Related Articles

Related Articles

Python | Pretty Print a dictionary with dictionary value

View Discussion
Improve Article
Save Article
  • Last Updated : 28 Aug, 2019

This article just provides a quick way to pretty print a dictionary which has dictionary as values. This is required many times now days as the advent of NoSQL databases. Lets code a way to perform this particular task.

Method : Using loops
We just loop through each dictionary element and it’s corresponding values using brute manner of loops.




# Python3 code to demonstrate working of
# Pretty Print a dictionary with dictionary value
# Using loops
  
# initializing dictionary
test_dict = {'gfg' : {'rate' : 5, 'remark' : 'good'}, 'cs' : {'rate' : 3}}
  
# printing original dictionary
print("The original dictionary is : " +  str(test_dict))
  
# using loops to Pretty Print
print("The Pretty Print dictionary is : ")
for sub in test_dict:
    print (sub)
    for sub_nest in test_dict[sub]:
        print (sub_nest, ':', test_dict[sub][sub_nest])

Output :

The original dictionary is : {'gfg': {'remark': 'good', 'rate': 5}, 'cs': {'rate': 3}}
The Pretty Print dictionary is : 
gfg
remark : good
rate : 5
cs
rate : 3
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!