90

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.

Georgy
  • 9,972
  • 7
  • 57
  • 66
mulllhausen
  • 4,069
  • 7
  • 45
  • 69
  • Does this answer your question? [How to pretty print nested dictionaries?](https://stackoverflow.com/questions/3229419/how-to-pretty-print-nested-dictionaries) – Georgy May 27 '20 at 11:14
  • @Georgy not quite. The desired output in that question is not the same as mine is here – mulllhausen May 29 '20 at 04:18

4 Answers4

112

Use width=1 or width=-1:

In [33]: pprint.pprint(a, width=1)
{'first': 123,
 'second': 456,
 'third': {1: 1,
           2: 2}}
Warren Weckesser
  • 102,583
  • 19
  • 173
  • 194
  • 1
    What do negative widths do? – Tim Nov 24 '13 at 05:26
  • 1
    @Tim: I just tried it, and it worked, but I don't know if that is documented somewhere. – Warren Weckesser Nov 24 '13 at 05:29
  • 2
    Negative widths don't do anything special. As the code goes along, it checks to see if the length of the partial output string is `< width`. Any negative width just means the answer will always be "no". A width of 0 will almost always (or maybe always - I don't care enough to think about perverse cases ;-)) do the same. – Tim Peters Nov 24 '13 at 06:04
  • 1
    @TimPeters: Yeah, I took a look at the source. There is a check for width==0 (it raises an error), but it seems that 0 could be treated the same as a negative value--always answer "no", as you put it--with no harm. – Warren Weckesser Nov 24 '13 at 06:26
47

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
}
binaryfunt
  • 5,618
  • 5
  • 33
  • 54
Ryan Chou
  • 996
  • 10
  • 20
  • 1
    Much prettier than the other solutions – Erik Aronesty Nov 30 '18 at 14:07
  • 5
    @ErikAronesty While you should pay attention to the `null` and boolean values (true/false). They are JSON values not the python objects. – Ryan Chou Dec 03 '18 at 02:56
  • 2
    Okay solution for simple cases, but converts python values to json values (as @RyanChou pointed out) and it might not be desirable or possible (like with `datetime`): `json.dumps({'d': datetime.now()})` will crash with `Object of type datetime is not JSON serializable`. – Andrey Dec 31 '19 at 11:48
  • `NameError: name 'json' is not defined` - Is this a module that needs importing? If so, would you mind updating your answer to reflect that? – Jack_Hu Apr 08 '21 at 11:05
28

If you are trying to pretty print the environment variables, use:

pprint.pprint(dict(os.environ), width=1)
UngodlySpoon
  • 301
  • 3
  • 5
4

Two things to add on top of Ryan Chou's already very helpful answer:

  • pass the 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.)
Zach Valenta
  • 1,488
  • 1
  • 17
  • 34
  • Neat! `sort_keys` is a great argument. Unfortunately, as you say not always applicable: `TypeError: Object of type Tag is not JSON serializable`... – PatrickT May 21 '20 at 20:55