![]() |
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: 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 FunctionIn 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
Output
{'name': 'GeeksforGeeks', 'website': 'www.horje.org', 'topics': ['Python', 'Java']} Print a Dictionary in Python Using For LoopIn 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
Output
name: GeeksforGeeks website: www.horje.org topics: ['Algorithms', 'Data Structures', 'Python', 'Java'] Print a Dictionary in Python Using pprint ModuleIn 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
Output
{'name': 'GeeksforGeeks', 'topics': ['Algorithms', 'Data Structures', 'Python', 'Java'], 'website': 'www.horje.org'} Print a Dictionary in Python Using f-StringIn 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
Output
{'name': 'GeeksforGeeks', 'website': 'www.horje.org', 'topics': ['Algorithms', 'Data Structures', 'Python', 'Java']} Print a Dictionary in Python Using List ComprehensionIn 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
Output
name: GeeksforGeeks website: www.horje.org topics: ['Algorithms', 'Data Structures', 'Python', 'Java'] ConclusionIn 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. |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |