My first thought was that the JSON serializer is probably pretty good at nested dictionaries, so I'd cheat and use that:

>>> import json
>>> print(json.dumps({'a':2, 'b':{'x':3, 'y':{'t1': 4, 't2':5}}},
...                  sort_keys=True, indent=4))
{
    "a": 2,
    "b": {
        "x": 3,
        "y": {
            "t1": 4,
            "t2": 5
        }
    }
}
Answer from Ken on Stack Overflow
🌐
Python
docs.python.org › 3 › library › pprint.html
pprint — Data pretty printer
Return the formatted representation of object as a string. indent, width, depth, compact, sort_dicts and underscore_numbers are passed to the PrettyPrinter constructor as formatting parameters and their meanings are as described in the documentation above.
Discussions

pretty printing dictionaries and evaluating strings into dicts
If you want a quick and dirty multiline representation, you can use Python's built-in pprint for pretty printing. If you want to have "nearly everything" in a seperate line, you can set the width parameter to 1, forcing pprint to put as few things in a single line as possible (normally, it tries to fit 80 chars in one line). Fro example, his examples are formatted only slightly different: >>> import pprint >>> pprint.pprint({'a': {'aa': 1, 'ab': 2, 'ac': ['aca', 'acb',]}, 'b': {'ba': 5, 'bb': 6}}, width=1) {'a': {'aa': 1, 'ab': 2, 'ac': ['aca', 'acb']}, 'b': {'ba': 5, 'bb': 6}} >>> d = {'num_company_results': 4607, 'start': 0, 'searchresults': [ {'title': '3D Systems Corporation', 'ticker': 'DDD', 'id': 557960, 'columns': [{'field': 'MarketCap', 'value': '1.15B'}, {'field': 'AverageVolume', 'value': 668631}], 'exchange': 'NYSE'}, {'title': '3M Company', 'ticker': 'MMM', 'id': 24599, 'columns': [ {'field': 'MarketCap', 'value': '60.79B'}, {'field': 'AverageVolume', 'value': '3110000'}], 'exchange': 'NYSE'}], 'results_type': 'COMPANY'} '} >>> pprint.pprint(d, width=1) {'num_company_results': 4607, 'results_type': 'COMPANY', 'searchresults': [{'columns': [{'field': 'MarketCap', 'value': '1.15B'}, {'field': 'AverageVolume', 'value': 668631}], 'exchange': 'NYSE', 'id': 557960, 'ticker': 'DDD', 'title': '3D ' 'Systems ' 'Corporation'}, {'columns': [{'field': 'MarketCap', 'value': '60.79B'}, {'field': 'AverageVolume', 'value': '3110000'}], 'exchange': 'NYSE', 'id': 24599, 'ticker': 'MMM', 'title': '3M ' 'Company'}], 'start': 0} Although I admit that the string splitting that occurs ("3D System Corporation") is a downside. If you omit the width parameter (it will be 80 then), pprint prints more into a single line, but it's still readable >>> pprint.pprint(d) {'num_company_results': 4607, 'results_type': 'COMPANY', 'searchresults': [{'columns': [{'field': 'MarketCap', 'value': '1.15B'}, {'field': 'AverageVolume', 'value': 668631}], 'exchange': 'NYSE', 'id': 557960, 'ticker': 'DDD', 'title': '3D Systems Corporation'}, {'columns': [{'field': 'MarketCap', 'value': '60.79B'}, {'field': 'AverageVolume', 'value': '3110000'}], 'exchange': 'NYSE', 'id': 24599, 'ticker': 'MMM', 'title': '3M Company'}], 'start': 0} If the data you are trying to pretty print is JSON compatible (no circular references, for example), you can also abuse the json module to create a pretty printed json string: >>> print(json.dumps(d, indent=True)) { "num_company_results": 4607, "start": 0, "results_type": "COMPANY", "searchresults": [ { "ticker": "DDD", "title": "3D Systems Corporation", "id": 557960, "columns": [ { "value": "1.15B", "field": "MarketCap" }, { "value": 668631, "field": "AverageVolume" } ], "exchange": "NYSE" }, { "ticker": "MMM", "title": "3M Company", "id": 24599, "columns": [ { "value": "60.79B", "field": "MarketCap" }, { "value": "3110000", "field": "AverageVolume" } ], "exchange": "NYSE" } ] } More on reddit.com
🌐 r/learnpython
1
6
May 11, 2014
Help to improve to this custom dict printing (or formatting) function?
I wonder what next-step improvements ... is used within a logger class (that inherits from Logger), so it’s not logging there itself. """Format dict for pretty printing.""" from collections.abc i...... More on discuss.python.org
🌐 discuss.python.org
4
0
December 19, 2024
Pretty Print Dictionary
Bless your soul. This is probably going to be my most used shortcut as a developer More on reddit.com
🌐 r/shortcuts
19
30
October 9, 2018
Printing a dictionary
Use a for loop to pull each key-value pair. You'll need to call the items() method on the dictionary to get both. Here's some code that will probably help: for key, value in odc.items(): print(f"{key} {value}") Assuming the dictionary in the code example above contains { "a": 1, "b": 2, "c": 3 } the above code will print out: a 1 b 2 c 3 More on reddit.com
🌐 r/learnpython
6
1
August 20, 2021
🌐
Coderwall
coderwall.com › p › gmxnqg › pretty-printing-a-python-dictionary
Pretty Printing a Python dictionary (Example)
October 22, 2022 - #python · #json · I just discovered this one yesterday. If you have a dictionary with more than a few items you need to be able to read, the json.dumps() function takes an indent argument that automatically formats things legibly for you: import json ... print json.dumps(my_dict, indent=1) You can even add sort_keys=True to...
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pretty-print-a-dictionary-with-dictionary-value
Python | Pretty Print a dictionary with dictionary value - GeeksforGeeks
July 12, 2025 - Space Complexity : Dependent on the size of the input dictionary. This method utilizes Python's `format` function to iterate through a dictionary (`my_dict`), printing each key-value pair with a customized template.
🌐
Code Beautify
codebeautify.org › python-formatter-beautifier
Python Formatter and Beautifier
Python Formatter helps to format unformatted or ugly Python data and helps to save and share Python.
🌐
GitHub
github.com › Textualize › rich › discussions › 1283
How to pretty print a dictionary inside a log · Textualize/rich · Discussion #1283
I want to beautifully print a dictionary. I want to get the output of from rich.pretty import pprint pprint(my_dict) But I want to log it as info, not a direct print. logging.info(my_dict) I have u...
Author: Textualize
Find elsewhere
🌐
FavTutor
favtutor.com › blogs › pretty-print-dictionary-python
Pretty Print Dictionary in Python: pprint & json (with examples)
February 13, 2023 - The Python pprint (pretty-print) module provides a function to display complex data structures like dictionaries in a more readable format.
🌐
Rich
rich.readthedocs.io › en › latest › pretty.html
Pretty Printing — Rich 14.1.0 documentation
In addition to syntax highlighting, Rich will format (i.e. pretty print) containers such as lists, dicts, and sets.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Pretty-print in Python: pprint | note.nkmk.me
May 17, 2023 - In Python, you can pretty-print objects such as lists (list) and dictionaries (dict) with the pprint module. pprint — Data pretty printer — Python 3.11.3 documentation Basic usage of pprint Specify ...
🌐
Reddit
reddit.com › r/learnpython › pretty printing dictionaries and evaluating strings into dicts
r/learnpython on Reddit: pretty printing dictionaries and evaluating strings into dicts
May 11, 2014 -

I know it's a pain to print these out, especially if they're deeply-nested and scrolling off the screen. This was something I tried and tried to hack around for a while, until I saw this guy's approach. This is actually a good learning point for hasattr and isinstance too. Check this out.

http://ze.phyr.us/python-dict-repr/

And then there's ast.literal_eval, which can be used to evaluate (safely) strings, turning them into dictionaries, if they're formatted properly. Cool! Have you guys used either of these in your projects?

Top answer
1 of 1
3
If you want a quick and dirty multiline representation, you can use Python's built-in pprint for pretty printing. If you want to have "nearly everything" in a seperate line, you can set the width parameter to 1, forcing pprint to put as few things in a single line as possible (normally, it tries to fit 80 chars in one line). Fro example, his examples are formatted only slightly different: >>> import pprint >>> pprint.pprint({'a': {'aa': 1, 'ab': 2, 'ac': ['aca', 'acb',]}, 'b': {'ba': 5, 'bb': 6}}, width=1) {'a': {'aa': 1, 'ab': 2, 'ac': ['aca', 'acb']}, 'b': {'ba': 5, 'bb': 6}} >>> d = {'num_company_results': 4607, 'start': 0, 'searchresults': [ {'title': '3D Systems Corporation', 'ticker': 'DDD', 'id': 557960, 'columns': [{'field': 'MarketCap', 'value': '1.15B'}, {'field': 'AverageVolume', 'value': 668631}], 'exchange': 'NYSE'}, {'title': '3M Company', 'ticker': 'MMM', 'id': 24599, 'columns': [ {'field': 'MarketCap', 'value': '60.79B'}, {'field': 'AverageVolume', 'value': '3110000'}], 'exchange': 'NYSE'}], 'results_type': 'COMPANY'} '} >>> pprint.pprint(d, width=1) {'num_company_results': 4607, 'results_type': 'COMPANY', 'searchresults': [{'columns': [{'field': 'MarketCap', 'value': '1.15B'}, {'field': 'AverageVolume', 'value': 668631}], 'exchange': 'NYSE', 'id': 557960, 'ticker': 'DDD', 'title': '3D ' 'Systems ' 'Corporation'}, {'columns': [{'field': 'MarketCap', 'value': '60.79B'}, {'field': 'AverageVolume', 'value': '3110000'}], 'exchange': 'NYSE', 'id': 24599, 'ticker': 'MMM', 'title': '3M ' 'Company'}], 'start': 0} Although I admit that the string splitting that occurs ("3D System Corporation") is a downside. If you omit the width parameter (it will be 80 then), pprint prints more into a single line, but it's still readable >>> pprint.pprint(d) {'num_company_results': 4607, 'results_type': 'COMPANY', 'searchresults': [{'columns': [{'field': 'MarketCap', 'value': '1.15B'}, {'field': 'AverageVolume', 'value': 668631}], 'exchange': 'NYSE', 'id': 557960, 'ticker': 'DDD', 'title': '3D Systems Corporation'}, {'columns': [{'field': 'MarketCap', 'value': '60.79B'}, {'field': 'AverageVolume', 'value': '3110000'}], 'exchange': 'NYSE', 'id': 24599, 'ticker': 'MMM', 'title': '3M Company'}], 'start': 0} If the data you are trying to pretty print is JSON compatible (no circular references, for example), you can also abuse the json module to create a pretty printed json string: >>> print(json.dumps(d, indent=True)) { "num_company_results": 4607, "start": 0, "results_type": "COMPANY", "searchresults": [ { "ticker": "DDD", "title": "3D Systems Corporation", "id": 557960, "columns": [ { "value": "1.15B", "field": "MarketCap" }, { "value": 668631, "field": "AverageVolume" } ], "exchange": "NYSE" }, { "ticker": "MMM", "title": "3M Company", "id": 24599, "columns": [ { "value": "60.79B", "field": "MarketCap" }, { "value": "3110000", "field": "AverageVolume" } ], "exchange": "NYSE" } ] }
🌐
W3Schools
w3schools.com › python › ref_module_pprint.asp
Python pprint Module
Pretty-print nested data in a readable ... "editor"]}]} pprint(data) Try it Yourself » · The pprint module formats Python objects in a way that's easier for humans to read....
🌐
Kanaries
docs.kanaries.net › topics › Python › pretty-print-dictionary-python
How to Use Pretty Print for Python Dictionaries – Kanaries
June 7, 2023 - Yet, their representation, particularly ... the day. The pprint module, standing for "pretty print", aims to provide more readable and aesthetically pleasing representations of complex data structures, like dictionaries....
🌐
Medium
medium.com › analytics-vidhya › pretty-printing-in-python-277bc4db2847
Pretty Printing In Python!. Dictionaries in Python can get long… | by Dhairye Kataria | Analytics Vidhya | Medium
February 11, 2024 - This is the same program and prints the same dictionary but now using the pprint() function from the pprint module instead.
🌐
Medium
medium.com › @parvez1487 › pretty-printing-a-dictionary-in-python-pythonpip-com-139be3808a9c
Pretty Printing a Dictionary in Python — pythonpip.com | by Parvez Alam | Medium
January 25, 2024 - This specifies that the indentation should be two spaces. The pprint aka “pretty print” is another Python module that is specifically used for enhancing the display of complex data structures in Python.
🌐
Python.org
discuss.python.org › python help
Help to improve to this custom dict printing (or formatting) function? - Python Help - Discussions on Python.org
December 19, 2024 - I wonder what next-step improvements ... is used within a logger class (that inherits from Logger), so it’s not logging there itself. """Format dict for pretty printing.""" from collections.abc i......
🌐
CodeGym
codegym.cc › java blog › learning python › pretty-print (pprint) in python
Pretty-Print (pprint) in Python
July 31, 2024 - The pprint module in Python is designed to provide a more human-readable output for complex data structures, such as dictionaries, lists, and nested combinations of these. In simpler terms, pprint formats your data neatly and clearly, making ...
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-pretty-print-python-dictionary-from-command-line
How to Pretty print Python dictionary from command line?
March 24, 2026 - The pprint module provides capability to "pretty-print" arbitrary Python data structures in a readable format, while the json module offers another elegant solution with customizable indentation.
🌐
Tistory
developer0hye.tistory.com › 414
[Python] Dict 예쁘게 출력하기
December 4, 2022 - https://stackoverflow.com/questions/3229419/how-to-pretty-print-nested-dictionaries How to pretty print nested dictionaries? How can I pretty print a dictionary with depth of ~4 in Python? I tried pretty printing with pprint(), but it did not work: import pprint pp = pprint.PrettyPrinter(indent=4) pp.pprint(mydict) I ... stackoverflow.com
🌐
Python
docs.python.org › 3 › library › json.html
json — JSON encoder and decoder
If sort_keys is true (default: False), then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis. If indent is a non-negative integer or string, then JSON array elements and object members will be pretty-printed with that indent level.
🌐
Reddit
reddit.com › r/shortcuts › pretty print dictionary
r/shortcuts on Reddit: Pretty Print Dictionary
October 9, 2018 -

I almost forgot how useful this one can be for dictionary, JSON and API work. Repost of something I developed last year.

Ever have trouble reading a dictionary or JSON data? I've been using dictionaries a lot lately and I got tired of having to copy/paste them to a website so I could read them. So taking advantage of the ability to run JavaScript in a shortcut using HTML, I give you... the Pretty Print Dictionary!

All you have to do is grab/create a dictionary, call this shortcut, and voila! An easy to read dictionary will be shown to you. Once you close the pop up, it will return the original data back so your original shortcut can continue running.

User u/Schl3ck extended my original work last year. If you pass the dictionary as part of a list, the remaining words in the list will be highlighted. You have to see it in action to appreciate how easy it makes finding a word in a huge amount of data.

Here’s the shortcut:
https://www.icloud.com/shortcuts/f73609147b8d4a888316c6e81a95cc94

And here’s an example shortcut that shows you how to use it:
https://www.icloud.com/shortcuts/4b0d038d93ef4e3abf613bf6212873b5

Enjoy!