Easily Understand Python pprint Function - Explained
Prettify Your Data Structures With Pretty Print in Python
Dealing with data is essential for any Pythonista, but sometimes that data is just not very pretty. Computers don’t care about formatting, but without good formatting, humans may find something hard to read. The output isn’t pretty when you use print()
on large dictionaries or long lists—it’s efficient, but not pretty.
The pprint
module in Python is a utility module that you can use to print data structures in a readable, pretty way. It’s a part of the standard library that’s especially useful for debugging code dealing with API requests, large JSON files, and data in general.
Understanding the Need for Python’s Pretty Print
Before you start exploring pprint
, you’ll first use urllib
to make a request to get some data. You’ll make a request to {JSON} Placeholder for some mock user information. The first thing to do is to make the HTTP GET
request and put the response into a dictionary:
Here, you make a basic GET
request and then parse the response into a dictionary with json.loads()
. With the dictionary now in a variable, a common next step is to print the contents with print()
:
When you run the above code, the output will not be easy to read. It will be a single horizontal line containing all the data. To make the output more readable, you can use the pprint
module.
Working With pprint
To use the pprint
module, you need to import it first:
Now, instead of using print()
, you can use pprint.pprint()
:
When you run the code, the output will be indented and formatted in a more readable way. Each item in the dictionary will be on a separate line, making it easier to view the data.
Exploring Optional Parameters of pprint()
The pprint()
function has several optional parameters that you can use to customize the output further. Here are some of the most commonly used parameters:
Summarizing Your Data: depth
The depth
parameter allows you to limit the depth to which pprint
will recurse. By default, it’s set to None
, which means it will recurse to any depth. If you only want to display a certain depth of your data, you can set the depth
parameter accordingly:
Giving Your Data Space: indent
The indent
parameter specifies the number of spaces to use for each level of indentation. By default, it’s set to 1
. If you want to increase or decrease the level of indentation, you can set the indent
parameter accordingly:
Limiting Your Line Lengths: width
The width
parameter specifies the maximum number of characters to display per line. By default, it’s set to 80
. If you want to adjust the maximum line length, you can set the width
parameter accordingly:
Squeezing Your Long Sequences: compact
The compact
parameter determines how to display long sequences, such as lists and tuples. When set to True
, it displays the entire sequence in a single line. When set to False
, it breaks the sequence into multiple lines. By default, it’s set to False
. If you want to display long sequences in a single line, you can set the compact
parameter to True
:
Directing Your Output: stream
The stream
parameter specifies the file-like object where the output should be directed. By default, it’s set to None
, which means it will use sys.stdout
as the output stream. If you want to direct the output to a different file-like object, you can set the stream
parameter accordingly:
Preventing Dictionary Sorting: sort_dicts
The sort_dicts
parameter determines whether dictionaries should be sorted alphabetically when printing. By default, it’s set to True
. If you want to preserve the original order of the dictionaries, you can set the sort_dicts
parameter to False
:
Prettifying Your Numbers: underscore_numbers
The underscore_numbers
parameter determines whether numbers should be prettified with underscores. By default, it’s set to False
. If you want to separate the digits of large numbers with underscores to make them more readable, you can set the underscore_numbers
parameter to True
:
Creating a Custom PrettyPrinter Object
In addition to using the pprint()
function, you can also create your own instance of the PrettyPrinter
class from the pprint
module. This allows you to have more control over the output formatting. Here’s an example of how you can create a custom PrettyPrinter
object:
By creating a custom PrettyPrinter
object, you can set parameters like indent
and width
according to your specific requirements.
Getting a Pretty String With pformat()
In addition to printing data structures, you can also get a pretty formatted string representation of the data using the pformat()
function from the pprint
module. Here’s an example:
The pformat()
function returns a string representation of the data structure in a pretty formatted way. You can then use that string for further processing or saving it to a file.
Handling Recursive Data Structures
Recursive data structures can be challenging to print without causing infinite loops. However, the pprint
module handles recursive data structures gracefully by detecting and representing them correctly. Here’s an example:
In this example, the pprint
module correctly represents the recursive structure of the friends
list without causing any infinite loops.
Conclusion
Python’s pprint
module is a powerful tool for printing data structures in a readable and pretty format. It allows you to customize the output by using optional parameters and even create your own instance of the PrettyPrinter
class. With pprint
, you can easily explore complex data structures, debug your code, and make your output more human-friendly.