Pretty Printing In Python!
![]()
D ictionaries in Python can get long, which often gets messy and makes it harder to read for a human. Just printing a dictionary onto the terminal doesn’t work. So, How can we make it readable for a programmer? Fortunately, There’s a library in the vast collection of libraries for python which can do exactly what we want. Let’s see how we can do this.
The pprint (“Pretty-Print”) Module:
The pprint module has some functions which are helpful to get a cleaner display of the items in the dictionary than what print() provides. The two functions are pprint() and pformat() . First, we will take a look over how the pprint() function can enhance the readability of a dictionary as compared to the print() function in a program and the pformat() function in the end.
print():
This is a program for counting the number of times a particular character has appeared in the given message and stores the result in the form of a dictionary and prints it by using the print() function. And the output we get from this program looks something like this:
Which is not readable at all but actually a disaster.
pprint():
This is the same program and prints the same dictionary but now using the pprint() function from the pprint module instead. Let’s see what magic does this function do:
This time, when the program is run, the output looks much cleaner, with the keys sorted.
How to print out a dictionary nicely in Python?
I’ve just started to learn python and I’m building a text game. I want an inventory system, but I can’t seem to print out the dictionary without it looking ugly.
This is what I have so far:
![]()
![]()
10 Answers 10
I like the pprint module (Pretty Print) included in Python. It can be used to either print the object, or format a nice string version of it.
But it sounds like you are printing out an inventory, which users will likely want shown as something more like the following:
My favorite way:
Here’s the one-liner I’d use. (Edit: works for things that aren’t JSON-serializable too)
Explanation: This iterates through the keys and values of the dictionary, creating a formatted string like key + tab + value for each. And "\n".join(. puts newlines between all those strings, forming a new string.
Python: Pretty Print a Dict (Dictionary) – 4 Ways

In this tutoria, you’ll learn how to use Python to pretty print a dict (dictionary). You’ll learn how to use the pprint library as well as the json library to do this. You’ll also learn how to pretty print nested dictionaries in Python. Finally, you’ll learn how to save a pretty printed dict in Python to a file.
Dictionaries are a key component of working with data in Python, including with data loaded from the internet. Many APIs return data in JSON format, which is very similar to the formatting of Python dictionaries. Dictionaries provide incredibly helpful ways in which you can store and retrieve data. However, they can be a bit tricky to read. Because of this, it’s often helpful to be able to print out your dictionaries in a more human-readable format. Without further ado, let’s take a look at what you’ll learn!
The Quick Answer: Use the json library

Table of Contents
What are Python Dictionaries?
Python dictionaries are data structures that use key:value pairs to hold and retrieve data. Keys are unique, in Python dictionaries, and must be made up of immutable data types (such as strings or integers), while values can be made up of anything, including additional dictionaries.
Let’s take a look at what a dictionary looks like in Python:
We can retrieve a value if we know a dictionary’s key. For example, if we wanted to retrieve the value for age , we could simply write: print(sample_dict.get(‘age’)) , which would return 31 .
Now that we have an understanding of what Python dictionaries are, let’s take a look at how to pretty print them.
Need to check if a key exists in a Python dictionary? Check out this tutorial, which teaches you five different ways of seeing if a key exists in a Python dictionary, including how to return a default value.
Pretty Print a Dict in Python with pprint
Python comes with a built-in library called pprint , which stands for pretty-print. Using this library, we can print out more nicely formatted data structures, including dictionaries.
The pprint library allows us to specify a number of different parameters, including the following:
- indent : the number of spaces to indent each line, where the default value is 1
- width : the maximum caharacters that are allowed on a single line, where the default is None (meaning, the maximum)
- depth : the number of levels to show while using nested data types, where the default is None (meaning that all levels of depth are shown)
- stream : used to specify an output stream and can be used to save to a file
- compact : If set to True , the values will be printed on single lines, to a certain width
- sort_dicts : if True , it prints the key-value pairs according to an alphabetical order of the keys. Defaults to True
Let’s load a dictionary and see how we can pretty print it using Python:
In the next section, you’ll learn how to use the JSON library to pretty print a Python dict.
Want to learn more about Python list comprehensions? Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.
Pretty Print a Dict in Python with JSON
As mentioned earlier, Python dictionaries carry a lot of similar characteristics with JSON objects. Because of this, we can use the JSON library to return a formatted JSON string.
Built into the json library is a function called .dumps() , which dumps a string object. Let’s take a look at what the .dumps() function looks like and what parameters it takes:
- sort_keys which sorts a dictionary’s keys and defaults to False
- indent : which lets Python know how many spaces to indent levels by
Let’s use the same dictionary as above and pretty print it using Python’s json library:
We can see here that this returns a very similar printed dictionary, with a key difference: the list is also printed on seperate lines, making it easier to read. This, of course, is a much prettier way to print your dictionaries.
In the next section, you’ll learn how to pretty print a nested dictionary in Python.
Want to learn how to pretty print a JSON file using Python? Learn three different methods to accomplish this using this in-depth tutorial here.
Pretty Print a Nested Dictionary in Python
In many cases, your dictionaries will be complex. You may even encounter dictionaries that contain other dictionaries. For example, you may have a dictionary that contains information on different people. Each key of the top-level dictionary will represent a person’s name and the value will be a different dictionary that will describe key attributes of a person.
Because of the complexity of dictionaries you may encounter, to better understand their structure, you may wish to print them out a way that more appropriately describes their structure.
Using the json library, this is actually very easy! It actually works in the same way as printing a normal dictionary. Let’s take a look:
In the next section, you’ll learn how to save a pretty printed dictionary to a file.
Save a Pretty Printed Dictionary to a File
Finally, let’s take a look at how to save a pretty printed dict to a file. We can use the pprint library to accomplish this, with the help of a context manager.
Let’s see how this can be done using Python:
We use a context manager using the open() function to link to a particular file where we want to save our dictionary and then use the ‘w’ parameter to say we want to write to the file. We then pass the dictionary and this file_name into the pprint() function to save it to a file.
Want to learn more about Python for-loops? Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.
Conclusion
In this post, you learned how to use Python to pretty print a dict. You learned how to do this using the pprint and json libraries, and learned the ways in which these two approaches differ. You also learned how to pretty print nested dictionaries in Python as well as how to save pretty printed dictionaries to a file.
To learn more about the pprint library, check out the official documentation here.
Python: Print items of a dictionary line by line (4 Ways)
In this article, we will discuss different ways to print line by line the contents of a dictionary or a nested dictionary in python.
As dictionary contains items as key-value pairs. So, first, let’s create a dictionary that contains student names and their scores i.e.
Now to print this dictionary, we directly pass it in the print function i.e.
the output will be like,
Although it printed the contents of the dictionary, all the key-value pairs printed in a single line. If we have big dictionaries, then it can be hard for us to understand the contents. Therefore, we should print a dictionary line by line. Let’s see how to do that,
Frequently Asked:
Print a dictionary line by line using for loop & dict.items()
dict.items() returns an iterable view object of the dictionary that we can use to iterate over the contents of the dictionary, i.e. key-value pairs in the dictionary and print them line by line i.e.
This approach gives us complete control over each key-value pair in the dictionary. We printed each key-value pair in a separate line.
Print a dictionary line by line by iterating over keys
We can iterate over the keys of a dictionary one by one, then for each key access its value and print in a separate line i.e.
Latest Python — Video Tutorial
Output:
Although by this approach we printed all the key value pairs line by line this is not an efficient method as compared to the previous one because to access one key-value pair, we are performing two operations.