Skip to content

#93: Pretty Print for Python

If you try out a Python package in the REPL, the output of print() may not be the most useful one. In Ruby I used for this case the class PrettyPrint and as it turns out, Python has a similar built-in feature. Let's have a look.

The problem with print()

The print() method works great for a single line of text that you want to write to the console. When you try to write a list or a dictionary, print() will write them in one single line. This list of dictionaries with nested dictionaries is a good example:

data = [
    {
        'name': "Rebecca Stephenson",
        'phone': "(154) 221-8558",
        'zipCode': "900185",
        'country': "South Korea",
        'options': ['a','b','c'],
        'total': "$74.79"
    },
    {
        'name': "Amos Nieves",
        'phone': "1-762-301-2264",
        'zipCode': "25566",
        'country': "Russian Federation",
        'options': {
            'a': 'full',
            'f': 'partial',
            'c': {'k1': 1,
                  'k2': 3}
        },
        'total': "$21.78"
    }
]

If we use print() for the output it looks like this:

1
2
3
>>> print(data)
[{'name': 'Rebecca Stephenson', 'phone': '(154) 221-8558', 'zipCode': '900185', 'country': 'South Korea', 'options': ['a', 'b', 'c'], 'total': '$74.79'}, {'name': 'Amos Nieves', 'phone': '1-762-301-2264', 'zipCode': 
'25566', 'country': 'Russian Federation', 'options': {'a': 'full', 'f': 'partial', 'c': {'k1': 1, 'k2': 3}}, 'total': '$21.78'}]

PrettyPrinter for Python

Python comes with a PrettyPrinter out of the box. All you need to do is to import pprint and instantiate the PrettyPrinter class:

import pprint
pp = pprint.PrettyPrinter()

The object from above looks a lot better when we print it with the PrettyPrinter:

>>> pp.pprint(data)
[{'country': 'South Korea',
  'name': 'Rebecca Stephenson',
  'options': ['a', 'b', 'c'],
  'phone': '(154) 221-8558',
  'total': '$74.79',
  'zipCode': '900185'},
 {'country': 'Russian Federation',
  'name': 'Amos Nieves',
  'options': {'a': 'full', 'c': {'k1': 1, 'k2': 3}, 'f': 'partial'},
  'phone': '1-762-301-2264',
  'total': '$21.78',
  'zipCode': '25566'}]

Options for PrettyPrinter

PrettyPrinter has a few options to influence the output. If we indent the objects and reduce the width for our output, PrettyPrinter may even better show the nested values:

>>> pp = pprint.PrettyPrinter(indent=5, depth=10, width=40, compact=False)
>>> pp.pprint(data)
[    {    'country': 'South Korea',
          'name': 'Rebecca Stephenson',
          'options': ['a', 'b', 'c'],
          'phone': '(154) 221-8558',
          'total': '$74.79',
          'zipCode': '900185'},
     {    'country': 'Russian Federation',
          'name': 'Amos Nieves',
          'options': {    'a': 'full',
                          'c': {    'k1': 1,
                                    'k2': 3},
                          'f': 'partial'},
          'phone': '1-762-301-2264',
          'total': '$21.78',
          'zipCode': '25566'}]

Conclusion

This little class is a great help when you need to inspect a dictionary in the console. We don't need to install any packages and can use it in any Python version released after 1997.