This tutorial will show you How to Print Dictionary in Python in an easy way. Python is a popular and strong programming language that can be used for a wide range of tasks, such as data analysis and manipulation. Printing dictionaries is a frequent task when working with data in Python. Dictionaries are extremely helpful for storing and organizing information since they are unordered collections of data in key-value pairs.
Print Dictionary in Python Using Print Module
Using this method, we import the print module and use it to print the input_dict’s contents without the need for looping. This module allows us to print the Dictionary in a format that is easier to read by printing each key-value pair on a separate line.
# input dictionary
input_dict = {
‘name’: ‘GeeksforGeeks’,
‘website’: ‘www.geeksforgeeks.org’,
‘topics’: [‘Algorithms’, ‘Data Structures’, ‘Python’, ‘Java’]
}
# printing using pprint module
pprint.pprint(input_dict)
Print Dictionary in Python Using For Loop
This method involves iterating through the key-value pairs in the input_dict using a for loop and printing each pair in the key: value format. The print statement is used to correctly print each dictionary pair.
# input dictionary
input_dict = {
‘name’: ‘GeeksforGeeks’,
‘website’: ‘www.geeksforgeeks.org’,
‘topics’: [‘Algorithms’, ‘Data Structures’, ‘Python’, ‘Java’]
}
# printing using For Loop
for key, value in input_dict.items():
print(f”{key}: {value}”)
Print Dictionary in Python Using List Comprehension
This method involves iterating through the key-value pairs in input_dict using list comprehension and printing each pair in key: value format on a separate line. Loop and this are comparable, but this is a more contemporary version.
# input dictionary
input_dict = {
‘name’: ‘GeeksforGeeks’,
‘website’: ‘www.geeksforgeeks.org’,
‘topics’: [‘Algorithms’, ‘Data Structures’, ‘Python’, ‘Java’]
}
# printing dict using list comprehension
[print(f”{key}: {value}”) for key, value in input_dict.items()]
Print Dictionary in Python Using f-String
With this method, we use a formatted string in Python to print the dictionary. A more readable way to print the entire dictionary is to use the formatted string f”{input_dict}”. When the string is printed, the real contents of the dictionary take the place of the {input_dict} portion.
# input dictionary
input_dict = {
‘name’: ‘GeeksforGeeks’,
‘website’: ‘www.geeksforgeeks.org’,
‘topics’: [‘Algorithms’, ‘Data Structures’, ‘Python’, ‘Java’]
}
# printing dict using Formatted String
print(f”{input_dict}”)
The Simplest Way: Using print()
Using the built-in print() function is the easiest and most direct way to print a dictionary. This will show the dictionary as a string with all of the keys and values included.
Using the fruit_colors dictionary as an example, here it is:
# Printing the dictionary using print()
print(fruit_colors)
# Output: {‘apple’: ‘red’, ‘banana’: ‘yellow’, ‘grape’: ‘purple’}
Printing Keys and Values Separately
You may want to print the dictionary’s values and keys separately in some circumstances. The keys() and values() methods, which return a list of the dictionary’s keys and values, respectively, can be used to accomplish this.
Here’s an example:
# Printing keys and values separately
print(“Keys:”, list(fruit_colors.keys()))
print(“Values:”, list(fruit_colors.values()))
# Output:
# Keys: [‘apple’, ‘banana’, ‘grape’]
# Values: [‘red’, ‘yellow’, ‘purple’]
Conclusion
When working with data, Print Dictionary in Python is a basic task. You can quickly show the contents of a dictionary in your Python programs by using the print() function. Furthermore, you can format the output for improved readability by iterating over the dictionary’s items—this is especially useful for dictionaries with a high number of key-value pairs. You’ll be ready to work with dictionaries in your Python projects if you have this knowledge.