How to PrettyPrint Nested Dictionaries?

PrettyPrint Nested Dictionaries

PrettyPrint is a module of python that is used to format complex data structures in a way that is easy for humans to read and interpret.

The pprint function of this module is used to format nested data structures like dictionaries, lists, and sets in a way that looks appealing and also understandable.

If you want to know more about the pretty print module, refer to this post.

We are most accustomed to using the print() function. Although both print() and pprint() have the same functionality that is to display something on the screen, the print() does not include any formatting and hence it just displays the output as it is without any beautification. This might create some hassle in the case of complex nested data structures like JSON.The pprint() is used to display nested data in a more structured way.

In this tutorial, we are going to focus on what are nested dictionaries and how we can prettyprint these dictionaries.

What Is a Dictionary?

A dictionary is a data structure in python that is used to store data in the form of a key: value pair.

A dictionary is an unordered and mutable data structure. A mutable data structure means it can be altered after creation. The elements of a dictionary are enclosed in curly braces.

Also read: Python Dictionary (Dict) Tutorial

Let us see an example of a dictionary.

dictn={"Food":['Apple Juice','Spaghetti','Rice']}

So in the above code, we are creating a variable called dictn to store the key-value pairs. The key in this dictionary is ‘Food’ and the elements in the square brackets are the values associated with the key.

In the next line, we are printing the dictionary.

Creating A Dictionary
Creating A Dictionary

Nested Dictionary Explained

A nested dictionary is a dictionary that has other dictionaries in it. The outer dictionary can contain as many inner dictionaries as possible.

Let us see an example.

stu = {
    "name": "John Walker",
    "age": 21,
    "courses": {
        "Compulsory courses":{ "Maths","Science","English" },
        "Electives":{"Applied Physics","Engineering Chemistry"   }  

This nested dictionary is a student record of a student named John Walker. It contains his name, age, and the courses he takes this semester.

Now it is a nested dictionary because, stu is the outer dictionary and courses is another dictionary which again consists of two other dictionaries Compulsory courses and Electives.

The values inside the curly braces are values associated with the keys.

Here is the output.

Nested Dictionary
Nested Dictionary

As you can see from the above output, the nested dictionary is just printed in one line without any formatting or beautification. It might also lead to some confusion when trying to read the output. Hence, it is necessary to use the PrettyPrint module to display the dictionary as appealing as possible.

The PrettyPrint Module

This module stands by its name- it displays the data structures in a more formatted and pretty way. This module is customizable so it is even easier for us to print the data in the way just we want to.

We are going to talk about the PrettyPrinter class and the pprint function and look at a few examples.

The PrettyPrinter Class

The PrettyPrinter is a class of the pprint module.

It has the following syntax.

class pprint.PrettyPrinter(indent=1, width=80, depth=None, stream=None, *, compact=False, sort_dicts=True, underscore_numbers=False)

We can create an instance of this class and then use the parameters of the class to print the nested dictionaries.

The parameters of the syntax are explained below.

indent=1: This field is used to specify the indentation between each nested level.

width=80: This field is used to determine the number of characters to be printed in a line. By default, there will be 80 characters in a line.

depth=None: This argument takes the number of levels that are to be included in the output. If this argument is set to None(default), no constraint is decided on the number of levels.

stream=None: This field is used to write the pretty printed output to a file-like object.

compact=False: Decides if the long sequences of lists, tuples, or sets should be formatted in a separate line or in each output line supported by the width parameter.

sort_dicts=True: As the name suggests, this parameter is used to sort the dictionaries by their keys. If it is set to False, the dictionaries are printed in the order of their creation.

underscore_numbers=False: This parameter is used to format the integers of the data structure with an underscore.

The pprint function

The syntax of the pprint and PrettyPrinter is almost the same. The very minute detail lying in the object parameter.

While the pprint function directly takes the object to be pretty printed, the PrettyPrinter class is called from the module to prettyprint some data

The syntax of the pprint is given below.

pprint.pprint(object, stream=None, indent=1, width=80, depth=None, *, compact=False, sort_dicts=True, underscore_numbers=False)

Here, the parameter object takes the data we wish to prettyprint as input.

Let us see how we can PrettyPrint a nested dictionary using the class and function.

PrettyPrint Nested Dictionary using pprint

Let us see how we can PrettyPrint a nested dictionary using the pprint function.

The nested dictionary is given below.

personslist = {'Person1': {'name': 'Jane', 'age': '27', 'Gender': 'Male','married':'No'},
          'Person2': {'name': 'Mary', 'age': '22', ''Gender': 'Female'},
         'Person3': {'name': 'Hyuna', 'age': '24', 'Gender': 'Female', 'married': 'No'},
          'Person4': {'name': 'Parker', 'age': '29', ''Gender': 'Male', 'married': 'Yes'}

As you can see, there is one main dictionary called personslist. There are 4 inner dictionaries Person1, Person2, Person3, and Person4. All the elements inside the curly braces in each line are the values associated with these keys.

Now let us try printing this nested dictionary like we normally do with print().

personslist = {'Person1': {'name': 'Jane', 'age': '27', 'Gender': 'Male','married':'No'},
          'Person2': {'name': 'Mary', 'age': '22', 'Gender': 'Female'},
         'Person3': {'name': 'Hyuna', 'age': '24', 'Gender': 'Female', 'married': 'No'},
          'Person4': {'name': 'Parker', 'age': '29', 'Gender': 'Male', 'married': 'Yes'}

The output is displayed in just one line and it becomes messy to look at and even interpret.


Let us try to print it with the help of pprint.

import pprint
personslist = {'Person1': {'name': 'Jane', 'age': '27', 'Gender': 'Male','married':'No'},
          'Person2': {'name': 'Mary', 'age': '22', 'Gender': 'Female'},
         'Person3': {'name': 'Hyuna', 'age': '24', 'Gender': 'Female', 'married': 'No'},
          'Person4': {'name': 'Parker', 'age': '29', 'Gender': 'Male', 'married': 'Yes'}

To use the pprint module, we are first required to import it to our environment which is being done in the first line.

Next, we have our nested dictionary personslist.

In the last line, we are directly using the pprint function of the pprint module hence it is pprint.pprint.

PrettyPrint NestedDictionary With pprint
PrettyPrint NestedDictionary With pprint

Look at the difference between the outputs! The first one has all the nested levels in just one line. While in the other output, the keys and values are placed hierarchically in separate lines.

Now we can even customize the nested levels. Let us check how to do that.

import pprint
personslist = {'Person1': {'name': 'Jane', 'age': '27', 'Gender': 'Male','married':'No'},
          'Person2': {'name': 'Mary', 'age': '22', 'Gender': 'Female'},
         'Person3': {'name': 'Hyuna', 'age': '24', 'Gender': 'Female', 'married': 'No'},
          'Person4': {'name': 'Parker', 'age': '29', 'Gender': 'Male', 'married': 'Yes'}

So in the last line, we used the width parameter. The width is set to 20 which means that, in each line, at most 20 characters are displayed.

PrettyPrint NestedDictionary With pprint(width)
PrettyPrint NestedDictionary With pprint(width)

PrettyPrint Nested Dictionary using PrettyPrinter

We have seen how to use the pprint function. Now let us see how we can create an instance of the PrettyPrinter class.

import pprint
personslist = {'Person1': {'name': 'Jane', 'age': '27', 'Gender': 'Male','married':'No'},
          'Person2': {'name': 'Mary', 'age': '22', 'Gender': 'Female'},
         'Person3': {'name': 'Hyuna', 'age': '24', 'Gender': 'Female', 'married': 'No'},
          'Person4': {'name': 'Parker', 'age': '29', 'Gender': 'Male', 'married': 'Yes'}

In the first line, we are importing the pprint module.

Next, we create a nested dictionary called personslist that contains the details of four persons under the main dictionary- personslist.

In the next line, we are creating an instance variable called pp for the PrettyPrinter class. We have also specified the width to be 30 which means in a line, 30 characters are included.

Lastly, we are pretty printing the dictionary using the pprint function.

PrettyPrint NestedDictionary With PrettyPrinter
PrettyPrint NestedDictionary With PrettyPrinter


The print() function just displays the data as it is in one line which does not look good when the data is a nested data structure.

In such cases, we can use the PrettyPrint module to print the nested data structures in a more pretty and formatted way.

In this tutorial, we saw the application of this module on nested dictionaries.

We have seen what a dictionary is and an example.

Next, we have seen the format of a nested dictionary with the help of an example.

We have discussed two parts of the pprint module- pprint function and PrettyPrinter class.

We have discussed their syntaxes, their parameters, and the main difference between them.

Next, we observed the difference between the print() and pprint() and how both of them display nested dictionaries.

Then, we also observed the usage of width to customize the output.

In the next example, we observed the usage of PrettyPrinter. We created an instance of this class which is then used to print the nested dictionary.


Visit the official Python documentation to know more about PrettyPrint.