Python | Pretty Print a dictionary with dictionary value

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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])

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.