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,

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.

Output:

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.

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.

Print a dictionary line by line using List Comprehension

In a single line using list comprehension & dict.items(), we can print the contents of a dictionary line by line i.e.

Output:

Print a dictionary line by line using json.dumps()

In python, json module provides a function json.dumps() to serialize the passed object to a json like string. We can pass the dictionary in json.dumps() to get a string that contains each key-value pair of dictionary in a separate line. Then we can print that string,

Output

We passed the dictionary object and count of indent spaces in json.dumps(). It returned a json like formatted string. Remember to import the json module for this approach.

Now, what if we have a nested python dictionary?

Printing nested dictionaries line by line in python

Suppose we have a nested dictionary that contains student names as key, and for values, it includes another dictionary of the subject and their scores in the corresponding subjects i.e.

If print this dictionary by passing it to the print() function,

Then the output will be like,

It printed all the contents in a single line. Therefore, it is tough to understand the contents. Now to print the contents of a nested dictionary line by line, we need to do double iteration i.e.

Output:

We first iterated over the items, i.e. key/value pairs of the dictionary, and for each pair printed the key. As value field is another dictionary, so we again iterated over the key-value pairs in this dictionary and printed its contents i.e. key/value pairs in separate lines.

Print nested dictionary line by line using json.dumps()

We can do this in a single line using json module’s dumps() function i.e.

Output:

The complete example is as follows,

Output:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Do Subscribe with us for more Articles / Tutorials like this,