How to Print Dictionary in Python?

Dictionary, a type of data structure that allows you to store key-value pairs, has a key role in Python.

Dictionaries in Python are important because they use key-value mapping format and hashing internally to quickly retrieve a value from the dictionary by its key.

There are several Python modules and functions that make printing dictionaries in a readable format easy.  In this blog post, we’ll explore some of the different ways that can be used to print dictionaries in Python.

  • Method 1: Using for Loop Method
  • Method 2: Using for Loop dict.items()
  • Method 3: Using dict.keys() Method
  • Method 4: Using List Comprehension
  • Method 5: Using json.dumps()
  • Method 6: Using itemgetter Module
  • Method 7: Using pprint.pprint() Function

Method 1: Using for Loop Method

The following code uses a for loop to iterate through the given dictionary and print the items/elements value:

Code:

value = {"Name": "Joseph", "Age": 22, "Height": 5.7}
for key in value:
  print(key, ":", value[key])

The for loop is used to iterate over the input dictionary and print each value of the key using print() function.

Output:

The dictionary has been printed.

Method 2: Using for Loop dict.items()

The for loop is used along with the “dict.items()” function in the below code to print dictionary items:

Code:

dict_value = {"Name": "Joseph", "Age": 22, "Height": 5.7}
for key, value in dict_value.items():
    print(key, ' : ', value)

The for loop is used with the “items()” function to print the elements by iterating over the given dictionary.

Output:

The dictionary has been displayed.

Method 3: Using dict.keys() Method

The “dict.keys()” is used to return an object containing the dictionary keys. The below code is used to print the dictionary:

Code:

dict_value = {"Name": "Joseph", "Age": 22, "Height": 5.7}
print(dict_value.keys())

The “dict.keys()” function is used to display all the keys of the dictionary.

Output:

The keys of the dictionary have been displayed.

Method 4: Using List Comprehension

The following code is used to display the dictionary elements/items using the list comprehension:

Code:

dict_value = {"Name": "Joseph", "Age": 22, "Height": 5.7}
[print(key,':',value) for key, value in dict_value.items()]

The list comprehension method is used with the for loop and “dict.items()” function to print the dictionary.

Output:

The input dictionary has been printed.

Method 5: Using json.dumps()

The json.dumps() method converts/transforms Python objects into JSON. The function can also be used to print simple dictionaries and nested dictionaries. Let’s see various examples used to print a dictionary using the json.dumps():

Example 1: Print Dictionary

The following example is used to print a normal dictionary:

Code:

import json
dict_value = {"Name": "Joseph", "Age": 22, "Height": 5.7}
print(json.dumps(dict_value))

The “json.dumps()” function takes the dictionary as an argument and retrieves the dictionary elements.

Output:

The dictionary element has been displayed.

Example 2: Print Nested Dictionary

The below example is used to print nested dictionary:

Code:

import json
dict_value = {"Name": "Joseph", "Age": 30, "Height": 5.7, "Subjects": {"Physics": 45, "Math": 77}}
print(json.dumps(dict_value))

The “json.dumps()” function takes the nested dictionary as an argument and retrieves the dictionary elements.

Output:

The nested dictionary element has been displayed.

Method 6: Using itemgetter Module

In Python, itemgetter is a module used to retrieve an item from a list or a dictionary. The following code uses the itemgetter module to print dictionary:

Code:

import operator
dict_value = {"Name": "Joseph", "Age": 22, "Height": 5.7}
for key, value in sorted(dict_value.items(), key=operator.itemgetter(0)):
    print(key, value)

The “sorted()” function is used to sort the dictionary items by key in ascending order and the “operator.itemgetter(0)” is used to extract the first element of each tuple (i.e., the key).

Output:

The dictionary has been printed.

Method 7: Using pprint.pprint() Function

The pprint.pprint() method in Python is used to pretty-print Python objects. This function can also print the dictionary elements. Here is an example code:

Code:

import pprint
dict_value = {"Name": "Joseph", "Age": 22, "Height": 5.7}
pprint.pprint(dict_value, width=4)

This function uses the width parameter to specify the maximum width of the output, and pretty-prints the dictionary on the console using “pprint.pprint()”.

Output:

The dictionary has been printed.

Conclusion

The “for loop” method, dict.items(), dict.keys(), list comprehension, json.dumps(), itemgetter module, and pprint.pprint() function is used in Python. The for loops are used to iterate over dictionary items and print their items/elements. Similarly, other methods such as “dict.items()” and “dict.keys()”, etc can print the elements and individual keys of the dictionary. This post presented various ways to print dictionaries in Python using numerous examples.