Skip to main content
Pretty Printing a Dictionary in Python

Pretty Printing a Dictionary in Python

In this article, I’ll demonstrate how to create a pretty print list of dictionaries in Python. You can learn how to print a dictionary as JSON in Python by using different techniques.

Python has two inbuilt methods that can be used to print a list of dictionaries pretty. The first one is json.dumps() and the second is the pprint.pprint() function.

What’s Pretty Print

Pretty printing dictionaries in Python means formatting them in a readable way. It organizes key-value pairs for better understanding.

What’s Dictionary

A dictionary is a mutable and unordered collection of key-value pairs. Each key must be unique and is associated with a corresponding value. Dictionaries are defined using curly braces {}, and elements are accessed using element keys.

We’ll cover the following techniques:

  • Using json.dumps()
  • Using print module
  • Using customized methods with recursive

Using json.dumps()

json.dumps() is a function included in the Python standard library that can be used for pretty printing. it is typically used for encoding Python objects as JSON.

Python Pretty Print Dictionary using json.dumps():

import json

myList = [{"id": 1, "name": "Adam", "Age" : 23}, {"id": 2, "name": "Joe", "Age" : 33}]

def pretty_print_dict(input_dict):
pretty_dict = json.dumps(input_dict, indent=2)
print(pretty_dict)

pretty_print_dict(myList);

Output:

[
{
"id": 1,
"name": "Adam",
"Age": 23
},
{
"id": 2,
"name": "Joe",
"Age": 33
}
]

In this example, We created a pretty_print_dict() method and defined one JSON list, and we also passed indent=2 parameter into the json.dumps() method. This specifies that the indentation should be two spaces.

Using pprint() Method

The pprint aka “pretty print” is another Python module that is specifically used for enhancing the display of complex data structures in Python. It is part of the Python standard library.

from pprint import pprint
myList = [{"id": 1, "name": "Adam", "Age" : 23}, {"id": 2, "name": "Joe", "Age" : 33}]
def pretty_print_dict(input_dict):
pprint(input_dict)

Output:

[
{
"id": 1,
"name": "Adam",
"Age": 23
},
{
"id": 2,
"name": "Joe",
"Age": 33
}
]

** Process exited - Return Code: 0 **
Press Enter to exit terminal

in the above example, We have used pprint() function to pretty print dictionary.

Custom Recursive Function

You can also use a customized approach for the pretty printing process. This approach involves iterating through the dictionary and applying indentation based on the level of nesting.

def custom_pretty_print(input_dict, indent=0):
 for key, value in input_dict.items():
  if isinstance(value, dict):
   print(' ' * indent + f'{key}:')
   custom_pretty_print(value, indent + 2)
 else:
  print(' ' * indent + f'{key}: {value}')

Example usage:

my_dict = {'name': 'John', 'age': 30, 'address': {'city': 'New York', 'zip': '10001'}}
custom_pretty_print(my_dict)

in this example, created a custom_pretty_print function that uses recursion to navigate through nested dictionaries. The indent parameter controls the level of indentation.

Conclusion:

Pretty printing dictionaries in Python are used to enhance the readability of complex data structures. We have discussed json.dumps(), the pprint module and customized method to print nested dictionaries, dictionaries containing lists, or large datasets.

Leave a Reply

Your email address will not be published. Required fields are marked *