How to pretty print nested dictionaries in Python

To pretty print nested dictionaries in Python:

  1. Use the JSON Serializer method
  2. Use the pprint module in Python
  3. Use the Yaml module in Python
  4. Use the rich module in Python
  5. Use yapf module in Python

Method No 1: Pretty print nested dictionaries in Python using the JSON Serializer

To pretty print nested dictionaries in Python, we can use the json.dumps() function. It can give you a good way to print the dictionary in python. Check the following example:

import json
n_dict = {'1': '1', '2': '2', '3': [1, 2, 3, 4, 5],
          '4': {'1': '1', '2': '2', 
                '3': [1, 2, 3, 4, 5]}}
p_dict = json.dumps(n_dict,sort_keys=True, indent=4)
print(p_dict)

# Output 👇
{
    "1": "1",
    "2": "2",
    "3": [
        1,
        2,
        3,
        4,
        5
    ],
    "4": {
        "1": "1",
        "2": "2",
        "3": [
            1,
            2,
            3,
            4,
            5
        ]
    }
}

Method No 2: Using the pprint module to pretty print nested Python dictionarries

To print a nested dictionary in a beautiful way using the pprint module in python. The pprint module in python is designed to print a dictionary in a beautiful form. See the following Example.

import pprint
n_dict = {'1': '1', '2': '2', '3': [1, 2, 3, 4, 5],
          '4': {'1': '1', '2': '2', 
                '3': [1, 2, 3, 4, 5]}}

pp= pprint.PrettyPrinter(depth=4)
pp.pprint(n_dict)


# Output 👇

# {'1': '1',
#  '2': '2',
#  '3': [1, 2, 3, 4, 5],
#  '4': {'1': '1', '2': '2', '3': [1, 2, 3, 4, 5]}}

Method No 3: Use Yaml module in Python to pretty print a nest dictionaries in Python

So the thing is that if the JSON didn’t work well, maybe you can try the yaml module in python.

import yaml

n_dict = {'1': '1', '2': '2', '3': [1, 2, 3, 4, 5],
          '4': {'1': '1', '2': '2', 
                '3': [1, 2, 3, 4, 5]}}

m_d=yaml.dump(n_dict, default_flow_style=False)
print(m_d)

Method No 4: Use the rich module in python to prettyprint a dictionary in Python

Rich library in python is specially designed to do tasks like this. It can be very helpful in print nested dictionaries. Check the following example:

from rich import print

n_dict = {'1': '1', '2': '2', '3': [1, 2, 3, 4, 5],
          '4': {'1': '1', '2': '2', 
                '3': [1, 2, 3, 4, 5]}}


print(n_dict)

Method No 5: Use the yapf module in python to prtty print a python nested dictionaries

You can also use yapf module in python to print nested dictionaries in python in a beautiful way. Check the following example.

from pprint import pformat
from yapf.yapflib.yapf_api import FormatCode

n_dict = {'1': '1', '2': '2', '3': [1, 2, 3, 4, 5],
          '4': {'1': '1', '2': '2', 
                '3': [1, 2, 3, 4, 5]}}
dict_string = pformat(n_dict)
formatted_code, _ = FormatCode(dict_string)

print(formatted_code)

# Output 👇

# {'1': '1',
#  '2': '2',
#  '3': [1, 2, 3, 4, 5],
#  '4': {'1': '1', '2': '2', '3': [1, 2, 3, 4, 5]}}

Leave a Comment

Scroll to Top