How to Print a Dictionary in Python
Last Updated :
22 Jan, 2024
In this article, we will learn how we can print a Dictionary in Python using various ways. Python Dictionaries are the form of data structures that allow us to store and retrieve the key-value pairs properly. While working with dictionaries, it is important to print the contents of the dictionary for analysis or debugging. We will explore all the possible ways with practical implementation to Print a Dictionary in Python. Below is an example for understanding the problem statement.
Example:
Input:
input_dict = {
'name': 'GeeksforGeeks',
'website': 'www.geeksforgeeks.org',
'topics': ['Algorithms', 'Data Structures', 'Python', 'Java']
}
Output:
name: GeeksforGeeks
website: www.geeksforgeeks.org
topics: ['Algorithms', 'Data Structures', 'Python', 'Java']
How to Print a Dictionary in Python?
There are different methods to Print a Dictionary in Python. Below we are explaining all the possible approaches with proper practical implementation.
Print a Dictionary in Python Using print Function
In this example, below code defines a dictionary named `input_dict` with key-value pairs and a list as a value, and then prints the dictionary using the `print` function.
Python3
input_dict = {
'name' : 'GeeksforGeeks' ,
'website' : 'www.geeksforgeeks.org' ,
'topics' : [ 'Python' , 'Java' ]
}
print (input_dict)
|
Output
{'name': 'GeeksforGeeks', 'website': 'www.geeksforgeeks.org', 'topics': ['Python', 'Java']}
Print a Dictionary in Python Using For Loop
In this approach, we are using a for loop to iterate over the key-value pairs of the input_dict, and we are printing each pair in the key: value format. Using the print statement, each of the dictionary pairs is properly printed.
Example: Below is the implementation of the above-discussed approach.
Python3
input_dict = {
'name' : 'GeeksforGeeks' ,
'website' : 'www.geeksforgeeks.org' ,
'topics' : [ 'Algorithms' , 'Data Structures' , 'Python' , 'Java' ]
}
for key, value in input_dict.items():
print (f "{key}: {value}" )
|
Output
name: GeeksforGeeks
website: www.geeksforgeeks.org
topics: ['Algorithms', 'Data Structures', 'Python', 'Java']
Print a Dictionary in Python Using pprint Module
In this approach, we are importing the pprint module and using it to print the contents of the input_dict without using any looping. Using this module, we print the dicoanory in a more readable format as each of the key: value pair is printed in separate lines.
Example: Below is the implementation of the above-discussed approach.
Python3
import pprint
input_dict = {
'name' : 'GeeksforGeeks' ,
'website' : 'www.geeksforgeeks.org' ,
'topics' : [ 'Algorithms' , 'Data Structures' , 'Python' , 'Java' ]
}
pprint.pprint(input_dict)
|
Output
{'name': 'GeeksforGeeks',
'topics': ['Algorithms', 'Data Structures', 'Python', 'Java'],
'website': 'www.geeksforgeeks.org'}
Print a Dictionary in Python Using f-String
In this approach, we are printing the dictionary using formatted string in Python.The formatted string f”{input_dict}” is used to print the entire dictionary in a more readable way. The {input_dict} part of the string is replaced with the actual contents of the dictionary when it is printed.
Example: Below is the implementation of the above-discussed approach.
Python3
input_dict = {
'name' : 'GeeksforGeeks' ,
'website' : 'www.geeksforgeeks.org' ,
'topics' : [ 'Algorithms' , 'Data Structures' , 'Python' , 'Java' ]
}
print (f "{input_dict}" )
|
Output
{'name': 'GeeksforGeeks', 'website': 'www.geeksforgeeks.org', 'topics': ['Algorithms', 'Data Structures', 'Python', 'Java']}
Print a Dictionary in Python Using List Comprehension
In this approach, we are using list comprehension to iterate over the key-value pairs of input_dict, and we are printing each pair in key:value format on a separate line. This is similar to For Loop but it is more modern than the traditional one.
Example: Below is the implementation of the above-discussed approach.
Python3
input_dict = {
'name' : 'GeeksforGeeks' ,
'website' : 'www.geeksforgeeks.org' ,
'topics' : [ 'Algorithms' , 'Data Structures' , 'Python' , 'Java' ]
}
[ print (f "{key}: {value}" ) for key, value in input_dict.items()]
|
Output
name: GeeksforGeeks
website: www.geeksforgeeks.org
topics: ['Algorithms', 'Data Structures', 'Python', 'Java']
Conclusion
In conclusion, printing dictionaries in Python is an important task for analyzing and debugging code. This article explored various methods, including using the print function, for loop, pprint module, formatted string, and list comprehension, providing a proper methods for developers to choose the most suitable approach based on their specific needs and preferences.
Share your thoughts in the comments
Please Login to comment...