About this question
How can you pretty print nested dictionaries in Python to make them more readable? What tools or methods can help you display complex dictionary structures in an easy-to-understand format?
How can you pretty print nested dictionaries in Python to make them more readable? What tools or methods can help you display complex dictionary structures in an easy-to-understand format?
Log in to share your answer and help other learners.
Log in to answerBest Answer · By JanBask Python Expert
Answered on May 15, 2025
Pretty printing nested dictionaries in Python is a great way to make complex data structures more readable and easier to understand. When dictionaries contain other dictionaries (nested dictionaries), the default print output can be hard to follow because everything is shown in one long line.
How to Pretty Print Nested Dictionaries
Python provides a built-in module called pprint (pretty-print) that formats data structures like dictionaries in a clean, indented style.
Using pprint module:
import pprint
nested_dict = {
'name': 'Alice',
'details': {
'age': 30,
'city': 'New York',
'skills': ['Python', 'SQL', 'Machine Learning']
},
'projects': {
'project1': {'status': 'complete', 'budget': 10000},
'project2': {'status': 'in progress', 'budget': 15000}
}
}
pprint.pprint(nested_dict)This will print the nested dictionary with proper indentation, line breaks, and spacing, making it much easier to read than the standard print() function.
Benefits of using pprint:
Alternative methods:
JSON module: You can convert the dictionary to a JSON string with indentation.
import json
print(json.dumps(nested_dict, indent=4))This works well especially if you want a format that’s easily shared or logged.
Summary:
Pretty printing helps you visually parse data better and is especially useful when debugging or presenting data.
Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.
Most-read guides across JanBask
Common Python interview questions, answered
Guides, tips and career advice on Python from JanBask experts.
Python How to Become a Python Developer: A Step-by-Step Guide for Beginners
Discover how to become a Python Developer via a practical 7-step guide build projects, maintain clean code, practice daily, and…
Python PCEP Certification Guide: Entry-Level Python Programmer Certification
Explore this PCEP certification guide for insights on cost, exam format, passing score, application, training, and study tips.…
Python Top 50+ Python Projects ideas for Beginners to Advanced
Discover innovative Python project ideas, explore advanced Python projects, and find good Python projects for beginners and…
Python How To Comment Out Multiple Lines In Python Like A Pro
Commenting out code is a common practice among programmers. It allows you to temporarily disable parts of your code without…