Sometimes, we want to pretty print nested dictionaries with Python.
In this article, we’ll look at how to pretty print nested dictionaries with Python.
How to pretty print nested dictionaries with Python?
To pretty print nested dictionaries with Python, we call json.dumps
with the indent
argument.
For instance, we write
import json
print(
json.dumps(
{"a": 2, "b": {"x": 3, "y": {"t1": 4, "t2": 5}}}, sort_keys=True, indent=4
)
)
to print the JSON string returned by json.dumps
.
We call it with indent
set to 4 to indent by 4 spaces in each level.
Conclusion
To pretty print nested dictionaries with Python, we call json.dumps
with the indent
argument.