7 minutes read

For programmers, it's essential to see the output of the variables regardless of the programming language. Sometimes, however, the output in the console isn't what we expect. You may have faced this problem when you tried to output a dictionary or a list variable to the console. Lucky for us, there are libraries to format the output of our variables! We don't need to worry about many loops and prints to format the output. One of such libraries is pprint. This library is a native Python library that you will meet in this topic.

Printing data

To start using it, import it first. Don't get confused because the method and the library have the same name:

from pprint import pprint

Then, we can use it as part of the usual print() method:

pprint("Hello pprint") # 'Hello pprint'

At least for now, you can spot no difference, except for the quotes. pprint retains quotes, while standard print does not. Also, the difference will be visible when you try to print more complex data structures such as lists or dictionaries.

Let's create a list with dictionaries inside and then print this list using the two methods:

dogs = [
    {
        "name": "Max",
        "breed": "Yorkshire",
        "age": 1,
        "owners": ["Susan, Camila, Paul"]
    },
    {
        "name": "Duke",
        "breed": "Bulldog",
        "age": 4,
        "owners": ["Thomas, David, Lucia"]
    },
    {
        "name": "Bella",
        "breed": "Poodle",
        "age": 2,
        "owners": ["Katharina, Arthur"]
    }
]

If we use the common print() method, the whole list will be printed on one line. It makes it rather clunky:

print(dogs)
[{'name': 'Max', 'breed': 'Yorkshire', 'age': 1, 'owners': ['Susan, Camila, Paul']}, ...]

If you refer to pprint(), each object will be printed on a new line:

from pprint import pprint

pprint(dogs)

[{'age': 1,
  'breed': 'Yorkshire',
  'name': 'Max',
  'owners': ['Susan, Camila, Paul']},
 {'age': 4,
  'breed': 'Bulldog',
  'name': 'Duke',
  'owners': ['Thomas, David, Lucia']},
 {'age': 2,
  'breed': 'Poodle',
  'name': 'Bella',
  'owners': ['Katharina, Arthur']}]

Much better! You may also notice that the dictionary was sorted by the key name. This happens because, by default, the sort_dicts property is True; we will discuss this property later on.

Properties

The name of this library stands for Pretty Printer. It allows you to format output. In this topic, we will focus on the pprint method.

Let's take a look at some settings and their default values.

  • stream=None — use this property when you want to write the content to a file-like object.;

  • indent=1 — number of spaces added for each nesting level (if it can't fit within the given width limit);

  • width=80 — maximum number of characters that can be printed on each line. If the line exceeds this limit, the remaining characters will be printed on a new line;

  • depth=None — maximum number of nesting levels that may be printed; anything beyond this level is replaced by ....;

  • compact=False — this property affects how long sequences (lists, tuples, sets, etc) are formatted. If it's False, each item is placed on a separate line; otherwise, it tries to fit items within a given width limit.;

  • sort_dicts=True — this property displays the key-value pairs of the dictionaries sorted by key.

  • underscore_numbers=False — if this property is True, integers will be formatted with the _ character as a thousand separator. However, if it is set to false (the default), no underscores will be displayed.

Keep in mind that these default values will be reset each time you invoke the method. We will discuss how you can define a global configuration later in this topic.

Indent and width

Let's start with the indent. We will set the value to 2. Each object will be aligned by 2 spaces:

from pprint import pprint

pprint(dogs, indent=2)

[ { 'age': 1,
    'breed': 'Yorkshire',
    'name': 'Max',
    'owners': ['Susan, Camila, Paul']},
  { 'age': 4,
    'breed': 'Bulldog',
    'name': 'Duke',
    'owners': ['Thomas, David, Lucia']},
  { 'age': 2,
    'breed': 'Poodle',
    'name': 'Bella',
    'owners': ['Katharina, Arthur']}]

Let's say you have a small screen, or you are working with several windows. If you don't want to scroll the terminal to the end of the line to see the data, you can limit the line width using the width property:

from pprint import pprint

pprint(dogs, width=20, indent=2)

[ { 'age': 1,
    'breed': 'Yorkshire',
    'name': 'Max',
    'owners': [ 'Susan, '
                'Camila, '
                'Paul']},
  { 'age': 4,
    'breed': 'Bulldog',
    'name': 'Duke',
    'owners': [ 'Thomas, '
                'David, '
                'Lucia']},
  { 'age': 2,
    'breed': 'Poodle',
    'name': 'Bella',
    'owners': [ 'Katharina, '
                'Arthur']}]

Even if a key-value pair exceeds the line size limit, it will be kept on the same line.

Depth, sort_dicts, and compact

What if you don't want to display the owner list? You can set the depth to 2, so the maximum number of nested objects is only two. Since the list of owners is inside a dictionary that is inside a list, it will not be displayed:

from pprint import pprint

pprint(dogs, depth=2)

[{'age': 1, 'breed': 'Yorkshire', 'name': 'Max', 'owners': [...]},
 {'age': 4, 'breed': 'Bulldog', 'name': 'Duke', 'owners': [...]},
 {'age': 2, 'breed': 'Poodle', 'name': 'Bella', 'owners': [...]}]

You can see that the contents of the owner list are replaced with dots. The data structure is maintained, so you can easily identify the object type.

Earlier, we have discussed that dictionaries are sorted by key name by default. If you want to preserve the insertion order, you need to change this property to False:

from pprint import pprint

pprint(dogs, depth=2, sort_dicts=False)

[{'name': 'Max', 'breed': 'Yorkshire', 'age': 1, 'owners': [...]},
 {'name': 'Duke', 'breed': 'Bulldog', 'age': 4, 'owners': [...]},
 {'name': 'Bella', 'breed': 'Poodle', 'age': 2, 'owners': [...]}]

sort_dicts has been first introduced in Python 3.8. If you try to use it in the previous version, it will raise the exception: TypeError: pprint() got an unexpected keyword argument 'sort_dicts'

The compact property is False by default. This property is best with lists, so let's create one:

phones = [
    ["iPhone 7", "iPhone X"], ["Samsung Galaxy S4", "Samsung Galaxy S7"], ["Huawei Mate 10 Pro", "Huawei Mate 9"]
]

If you print it as it is, this will be the result:

from pprint import pprint

pprint(phones)

[['iPhone 7', 'iPhone X'],
 ['Samsung Galaxy S4', 'Samsung Galaxy S7'],
 ['Huawei Mate 10 Pro', 'Huawei Mate 9']]

Each list has been printed on a new line. We can now work on visualization. However, if you want to compress the output, change the compact property to True, and the values will be compressed along the lines, as long as they do not exceed the width limit.

Let's see an example:

from pprint import pprint

pprint(phones, compact=True)

[['iPhone 7', 'iPhone X'], ['Samsung Galaxy S4', 'Samsung Galaxy S7'],
 ['Huawei Mate 10 Pro', 'Huawei Mate 9']]

As you can see, everything is printed in two lines.

Setting a global configuration

If you don't want to set the properties every time you invoke the pprint method, you can import the PrettyPrinter class constructor and create an object with the desired settings. If you need to change some settings, you can change them in one place.

First, you need to import the constructor from the library:

from pprint import PrettyPrinter

Then, create an instance of this class with the settings you want:

pp = PrettyPrinter(indent=4, depth=2, sort_dicts=False)

Now, all you have to do is to call the pprint method from the object you've just created:

pp.pprint(dogs)

[   {'name': 'Max', 'breed': 'Yorkshire', 'age': 1, 'owners': [...]},
    {'name': 'Duke', 'breed': 'Bulldog', 'age': 4, 'owners': [...]},
    {'name': 'Bella', 'breed': 'Poodle', 'age': 2, 'owners': [...]}]

Conclusion

In this topic, you have learned about the pprint library and its purpose. You can now format the output of your data to print complex data structures without struggling to visualize them.
You have also learned that you can use the pprint method and define the property each time you want to use it. You can also create an instance of the PrettyPrinter class and define the global settings for the method. If you are eager to learn more, you can always check out the Official Documentation.

93 learners liked this piece of theory. 1 didn't like it. What about you?
Report a typo