How to Print a Dictionary in Python?

As a Python developer working on a project for a client in the USA, I recently encountered a situation where I needed to display the contents of a dictionary in a clear and readable format. After researching and testing various methods, I discovered several effective ways to accomplish this task. In this tutorial, I will explain how to print a dictionary in Python with examples.

Print a Dictionary in Python

Before we get into printing dictionaries, let’s briefly review what dictionaries are in Python. A dictionary is a collection of key-value pairs, where each key is unique and associated with a specific value. Dictionaries are mutable, meaning you can modify their contents after creation.

Here’s an example of a dictionary containing information about a person:

person = {
    "name": "John Doe",
    "age": 35,
    "city": "New York"
}

Read How to Sort a Python Dictionary by Key Alphabetically?

Method 1. Use the print() Function

The simplest way to print a dictionary in Python is by using the built-in print() function. You can directly pass the dictionary to print() , and it will display the dictionary’s contents in its default format.

person = {
    "name": "Sarah Johnson",
    "age": 28,
    "city": "Los Angeles"
}

print(person)

Output:

{'name': 'Sarah Johnson', 'age': 28, 'city': 'Los Angeles'}

I executed the above example code and added the screenshot.

Print a Dictionary in Python

As you can see, the print() function displays the dictionary’s key-value pairs enclosed in curly braces {}.

Print a Dictionary with Formatted Output

While using print() directly is quick and easy, the output might not be very readable, especially for larger Python dictionaries. To improve readability, you can use a combination of loops and formatted strings to print the dictionary’s contents.

employee = {
    "name": "Michael Smith",
    "position": "Manager",
    "salary": 80000,
    "department": "Sales"
}

for key, value in employee.items():
    print(f"{key}: {value}")

Output:

name: Michael Smith
position: Manager
salary: 80000
department: Sales

I executed the above example code and added the screenshot.

How to Print a Dictionary in Python

In this example, we iterate over the dictionary’s key-value pairs using the items() method and print each pair on a separate line using an f-string.

Check out How to Find Max Value in Python Dictionary

Method 2. Use the pprint Module for Pretty Printing

Python provides a built-in module called pprint (pretty print) that offers a more visually appealing way to display dictionaries. The pprint module automatically formats the dictionary’s contents with proper indentation and line breaks.

from pprint import pprint

customer = {
    "name": "Emily Davis",
    "email": "emily@example.com",
    "orders": [
        {"id": 1, "product": "Shirt", "price": 29.99},
        {"id": 2, "product": "Jeans", "price": 59.99}
    ],
    "address": {
        "street": "123 Main St",
        "city": "Chicago",
        "state": "IL"
    }
}

pprint(customer)

Output:

{'address': {'city': 'Chicago', 'state': 'IL', 'street': '123 Main St'},
 'email': 'emily@example.com',
 'name': 'Emily Davis',
 'orders': [{'id': 1, 'price': 29.99, 'product': 'Shirt'},
            {'id': 2, 'price': 59.99, 'product': 'Jeans'}]}

I executed the above example code and added the screenshot.

Print a Dictionary in Python pprint Module

The pprint module is particularly useful when dealing with nested dictionaries or dictionaries containing complex structures.

Read How to Find Duplicate Values in Dictionary Python

Print Specific Dictionary Elements

Sometimes, you may only need to print specific elements of a dictionary rather than the entire contents. You can access individual values by their corresponding keys.

student = {
    "name": "David Wilson",
    "age": 22,
    "major": "Computer Science",
    "gpa": 3.8
}

print("Student Name:", student["name"])
print("Major:", student["major"])

Output:

Student Name: David Wilson
Major: Computer Science

In this example, we directly access the values associated with the keys “name” and “major” using square bracket notation.

Check out Iterate through Dictionary with multiple values in Python

Conclusion

In this tutorial, I have explained how to print a dictionary in Python. I discussed the print() function, printing a dictionary with a formatted function, and the pprint module for pretty printing. I also covered how to print specific dictionary elements.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.