How to pretty print nested dictionaries in Python

0

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]}}



Have a Chat With Me!

I hope you liked this Python Tutorial๐Ÿ’—. Now, Don't hesitate to leave a message at haxratali0@gamil.com. I respond to every mail I receive. I will be more than happy to chat with you.

Post a Comment

0Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !