There’s a built-in module in Python called pprint it allows you to “pretty print” data structures in Python. To use it import the module, and wrap anything you want to print with pprint
instead of print
.
import pprint
d = {
'name': 'paul',
'job': 'swe',
'likes': ['coffee', 'tea', 'rice']
}
# using pprint
pprint.pprint(d, width=2)
Remember to force width if you have a dictionary that fits on a single line. From the docs
Single line dictionaries stay as a single line.
The formatted representation keeps objects on a single line if it can, and breaks them onto multiple lines if they don’t fit within the allowed width. ConstructPrettyPrinter
objects explicitly if you need to adjust the width constraint.