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.
# Python3 code to demonstrate working of# Pretty Print a dictionary with dictionary value# Using loops # initializing dictionarytest_dict = {'gfg' : {'rate' : 5, 'remark' : 'good'}, 'cs' : {'rate' : 3}} # printing original dictionaryprint("The original dictionary is : " + str(test_dict)) # using loops to Pretty Printprint("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


