Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to Print Dictionary Keys in Python
Next article icon

How to Print a Dictionary in Python

Last Updated : 27 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Example: Using print Function

Python
# input dictionary input_dict = {     'name': 'GeeksforGeeks',     'website': 'www.geeksforgeeks.org',     'topics': [ 'Python', 'Java'] } # printing dict using print function print(input_dict) 

Output
{'name': 'GeeksforGeeks', 'website': 'www.geeksforgeeks.org', 'topics': ['Python', 'Java']} 

Explanation: In this example, the 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.

Table of Content

  • Using For Loop
  • Using pprint Module
  • Using f-String
  • Using List Comprehension

We will explore all the possible ways with practical implementation to Print a Dictionary in Python.

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.

Python
# 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}") 

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.

Python
import pprint # input dictionary input_dict = {     'name': 'GeeksforGeeks',     'website': 'www.geeksforgeeks.org',     'topics': ['Algorithms', 'Data Structures', 'Python', 'Java'] } # printing using pprint module 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 F- 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.

Python
# 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}") 

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.

Python
# 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()] 

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.


Next Article
How to Print Dictionary Keys in Python

G

gauravggeeksforgeeks
Improve
Article Tags :
  • Python
  • Python Programs
  • Geeks Premier League
  • python-dict
  • Geeks Premier League 2023
Practice Tags :
  • python
  • python-dict

Similar Reads

  • How to Print Dictionary Keys in Python
    We are given a dictionary and our task is to print its keys, this can be helpful when we want to access or display only the key part of each key-value pair. For example, if we have a dictionary like this: {'gfg': 1, 'is': 2, 'best': 3} then the output will be ['gfg', 'is', 'best']. Below, are the me
    2 min read
  • How to Initialize Dictionary in Python
    In Python dictionary, each key is unique and we can store different data types as values. The simplest and most efficient method to initialize a dictionary is using curly braces {}. This is the easiest and most common way to create a dictionary. We can use curly braces '{} ' and separate the keys an
    2 min read
  • How to Update a Dictionary in Python
    This article explores updating dictionaries in Python, where keys of any type map to values, focusing on various methods to modify key-value pairs in this versatile data structure. Update a Dictionary in PythonBelow, are the approaches to Update a Dictionary in Python: Using with Direct assignmentUs
    3 min read
  • How to Reverse a Dictionary in Python
    Reversing a dictionary typically means swapping the keys and values, where the keys become the values and the values become the keys. In this article, we will explore how to reverse a dictionary in Python using a variety of methods. Using Dictionary ComprehensionDictionary comprehension is a concise
    2 min read
  • How to Add User Input To A Dictionary - Python
    The task of adding user input to a dictionary in Python involves taking dynamic data from the user and storing it in a dictionary as key-value pairs. Since dictionaries preserve the order of insertion, we can easily add new entries based on user input. For instance, if a user inputs "name" as the ke
    4 min read
  • How to Print a Tab in Python
    In Python, printing a tab space is useful when we need to format text, making it more readable or aligned. For example, when displaying data in columns, we might want to add a tab space between the values for a cleaner appearance. We can do this using simple methods like \t, the print() function or
    2 min read
  • How to Add Values to Dictionary in Python
    The task of adding values to a dictionary in Python involves inserting new key-value pairs or modifying existing ones. A dictionary stores data in key-value pairs, where each key must be unique. Adding values allows us to expand or update the dictionary's contents, enabling dynamic manipulation of d
    3 min read
  • Convert a Dictionary to a List in Python
    In Python, dictionaries and lists are important data structures. Dictionaries hold pairs of keys and values, while lists are groups of elements arranged in a specific order. Sometimes, you might want to change a dictionary into a list, and Python offers various ways to do this. How to Convert a Dict
    3 min read
  • Loop Through a Nested Dictionary in Python
    Working with nested dictionaries in Python can be a common scenario, especially when dealing with complex data structures. Iterating through a nested dictionary efficiently is crucial for extracting and manipulating the desired information. In this article, we will explore five simple and generally
    3 min read
  • How to Compare Two Dictionaries in Python
    In this article, we will discuss how to compare two dictionaries in Python. The simplest way to compare two dictionaries for equality is by using the == operator. Using == operatorThis operator checks if both dictionaries have the same keys and values. [GFGTABS] Python d1 = {'a': 1, 'b
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences