Skip to content
Related Articles

Related Articles

Improve Article

Python | Pretty Print a dictionary with dictionary value

  • 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

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :