Print Dictionary Line by Line in Python

Python provides four data types for collecting and storing data. These data types are also known as Python Collections. One of these four data types is a dictionary.

A dictionary is capable of storing the data in key:value pairs, due to which there are certain styles in which it can be printed. This tutorial focuses on and demonstrates the different ways available to print a dictionary line by line in Python.

One of the four collection data types, a dictionary is a changeable and ordered collection that is able to store and retain data in key:value pairs. Much like a set, a dictionary does not allow any duplicity.

Important: Dictionaries used to be unordered before Python 3.7. In all the newer versions of Python after the 3.7 release, all dictionaries are ordered.

How To Print Dictionary Line by Line in Python?

There are four ways that can assist with the task of printing a dictionary line by line in Python, with all four of them being a little different from each other. All the ways are thoroughly described in the article below.

Using the dict.items() function along with the for loop

The dict.items() function is utilized to get a more readable and iterable view of the dictionary objects by which we can iterate over all the key:value pairs in the dictionary. This function alone though, would not get us the optimum result.

The dict.items() function, when utilized with the for loop is able to properly access both the key and the value pairs and is able to print them line by line.

The following code uses the dict.items() function along with the for loop to print a dictionary line by line in Python.

The above code provides the following output:

Rahul : 24
Rohit : 52
Kohli : 5
Hardik : 30
Dhoni : 34

The advantage of making use of this method is that the control over each key:value pair is totally in the user’s hands.

Using the dict.items() functions along with List Comprehension

List Comprehension aids in making the core more compact and is an easier way to create lists with reference to an already existing list.

However, apart from creating lists, when List comprehension is utilized with the dict.items() function, it can implement the task of printing a dictionary line by line in Python.

The following code uses the dict.items() functions along with List Comprehension to print a dictionary line by line in Python.

The above code provides the following output:

Rahul : 24
Rohit : 52
Kohli : 5
Hardik : 30
Dhoni : 34

Iterating over keys to print a dictionary line by line in Python.

This is a more unconventional method as compared to the one mentioned above, as it involves two separate operations which need to be understood and implemented.

In this way, we iterate over each key one by one, and post accessing the certain key, we access the value assigned with it and print both of them in a separate line.

The following code iterates over keys to print a dictionary line by line in Python.

The above code provides the following output:

Rahul : 24
Rohit : 52
Kohli : 5
Hardik : 30
Dhoni : 34

Using the json.dumps() function

JSON is a big part of the Python programming world and stands for JavaScript Object Notation. It has a crucial role in major areas of Python programming and is basically a syntax that is utilized for the purpose of storing and exchanging data.

One such function provided by the JSON module is the json.dumps() function that is able to serialize the given object into a string that is similar to a JSON string.

In other words, this function passes the dictionary and a string is generated that contains all the key:value pairs in the given dictionary in separate lines. This string can then easily be printed with the generic print function.

The JSON module needs to be imported to the Python code first in order to implement this method.

The following code uses the json.dumps() function to print a dictionary line by line in Python.

The above code provides the following output:

{
“Rahul”: 24,
“Rohit”: 52,
“Kohli”: 5,
“Hardik”: 30,
“Dhoni”: 34
}

We should note that here in this function, it is important to mention the count of indents. If not mentioned, it might print all the key:value pairs in the same line.

How To Print a Nested Dictionary Line by Line in Python?

When dealing with a nested dictionary, the generic ways to print a dictionary line by line would not work. However, this task can be achieved in two ways, both of which will be described in this article below.

As an example, we will be taking a nested dictionary that contains the names of the countries against which Team India plays the match as the key, and the values include another dictionary that has the name of players of Team India and their respective scores in that match.

Using a nested for loop along with the dict.items() function

When dealing with a nested dictionary, it is not enough to iterate over the dictionary once. First, we have to iterate over the parent dictionary, and then move on to iterating over the nested dictionary.

The dict.items() function comes in handy while making both the for loops.

The following code uses a nested for loop along with the dict.items() function to print a nested dictionary line by line in Python.

The above code provides the following output:

vs Aus —
Kohli : 50
Rohit : 7
Dhoni : 34
Hardik : 42
vs Pak —
Kohli : 23
Rohit : 76
Dhoni : 2
Hardik : 28
vs Eng —
Kohli : 3
Rohit : 22
Dhoni : 57
Hardik : 66

Using the json.dumps() function

The json.dumps() function makes it possible to implement the given task at hand with the help of a single line of code, without any hassle.

The indent parameter is a must and should be set at a decent value as per the programmer’s needs.

The JSON module needs to be imported to the python code first in order to implement this method.

The following code uses the json.dumps() function to print a nested dictionary line by line in Python.

The above code provides the following output:

{
“vs Aus”: {
“Kohli”: 50,
“Rohit”: 7,
“Dhoni”: 34,
“Hardik”: 42
},
“vs Pak”: {
“Kohli”: 23,
“Rohit”: 76,
“Dhoni”: 2,
“Hardik”: 28
},
“vs Eng”: {
“Kohli”: 3,
“Rohit”: 22,
“Dhoni”: 57,
“Hardik”: 66
}
}

Conclusion.

This article provides brushes up and provides basic insight into dictionaries and how to print a dictionary line by line in Python. Firstly, we cover how to print dictionary line by line in Python. Then, we move our focus to nested dictionaries and cover how to print a nested dictionary line by line in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *