Reputation: 4435
I'm trying to get a pretty print of a dictionary, but I'm having no luck:
>>> import pprint
>>> a = {'first': 123, 'second': 456, 'third': {1:1, 2:2}}
>>> pprint.pprint(a)
{'first': 123, 'second': 456, 'third': {1: 1, 2: 2}}
I wanted the output to be on multiple lines, something like this:
{'first': 123,
'second': 456,
'third': {1: 1,
2: 2}
}
Can pprint
do this? If not, then which module does it? I'm using Python 2.7.3.
Upvotes: 102
Views: 94897
Reputation: 11232
As an alternative to pprint
, beginning from Python 3.12, you can use the built-in reprlib
module for pretty-printing:
>>> from reprlib import Repr
>>> example = {'first': 123, 'second': 456, 'third': {1: 1, 2: 2}}
>>> print(Repr(indent=4).repr(example))
{
'first': 123,
'second': 456,
'third': {
1: 1,
2: 2,
},
}
>>>
You can use the maxlevel
keyword argument to decide how deep a nested dictionary should be printed:
>>> print(Repr(indent=4, maxlevel=1).repr(example))
{
'first': 123,
'second': 456,
'third': {...},
}
>>>
Upvotes: 1
Reputation: 3616
This is a Copy-Pasta for testing purposes and to help with a usage example.
from pprint import pprint # I usually only need this module from the package.
a = {'first': 123, 'second': 456, 'third': {1:1, 2:2}, 'zfourth': [{3:9, 7:8}, 'distribution'], 1:2344, 2:359832, 3:49738428, 4:'fourth', 5:{'dictionary':'of things', 'new':['l','i','s','t']}}
pprint(dict(a), indent=4, width=1)
# Wrap your variable in dict() function
# Optional: indent=4. for readability
# Required: width=1 for wrapping each item to its own row.
# Note: Default pprint is to sort the dictionary
# Note: This also auto-wraps anything sting that has spaces in it. See 'of things' below.
# Documentation: https://docs.python.org/3/library/pprint.html
# Examples: https://pymotw.com/2/pprint/
# Blog: https://realpython.com/python-pretty-print/
Provides the following result:
{ 1: 2344,
2: 359832,
3: 49738428,
4: 'fourth',
5: { 'dictionary': 'of '
'things',
'new': [ 'l',
'i',
's',
't']},
'first': 123,
'second': 456,
'third': { 1: 1,
2: 2},
'zfourth': [ { 3: 9,
7: 8},
'distribution']}
Upvotes: -1
Reputation: 1132
You could convert the dict to json through json.dumps(d, indent=4)
import json
print(json.dumps(item, indent=4))
{
"second": 456,
"third": {
"1": 1,
"2": 2
},
"first": 123
}
Upvotes: 56
Reputation: 1879
Two things to add on top of Ryan Chou's already very helpful answer:
sort_keys
argument for an easier visual grok on your dict, esp. if you're working with pre-3.6 Python (in which dictionaries are unordered)print(json.dumps(item, indent=4, sort_keys=True))
"""
{
"first": 123,
"second": 456,
"third": {
"1": 1,
"2": 2
}
}
"""
dumps()
will only work if the dictionary keys are primitives (strings, int, etc.)Upvotes: 4
Reputation: 309
If you are trying to pretty print the environment variables, use:
pprint.pprint(dict(os.environ), width=1)
Upvotes: 28
Reputation: 114811
Use width=1
or width=-1
:
In [33]: pprint.pprint(a, width=1)
{'first': 123,
'second': 456,
'third': {1: 1,
2: 2}}
Upvotes: 129