Force vertical display with pprint
<p>I'm using <code>pprint</code> to display the <code>diff</code> between two dictionaries.  Sometimes the values in the dictionary are long strings, which I want to see as single lines, so I set the <code>width</code> to some large value.  Unfortunately, sometimes these dictionary are shallow (i.e., while in the general case they can embed lists and dictionaries, some are just dictionaries with short string values) in which case <code>pprint</code> "pretty-prints" them on a single line:</p>

<pre><code>&gt;&gt;&gt; pprint.pprint({'a': 'a', 'b': 'b'})
{'a': 'a', 'b': 'b'}
</code></pre>

<p>Is there a means to force <code>pprint</code> to always display lists and dictionaries vertically?  Again <code>width</code> is not an option as because of it strings are split inside lines (i.e., breaking on <code>\n</code> is good, but not elsewhere).</p>

<pre><code>&gt;&gt;&gt; pprint.pprint({'a': 'a ' * 50}, width=20)
{'a': 'a a a a a a '
      'a a a a a a '
      'a a a a a a '
      'a a a a a a '
      'a a a a a a '
      'a a a a a a '
      'a a a a a a '
      'a a a a a a '
      'a a '}
</code></pre>

<p>If <code>pprint</code> is not suitable, is there some standard library that would save me from writing a dedicated routine?</p>

<h2>Option: Use <code>json</code></h2>

<p>I found converting the dictionary to JSON more predictable than <code>pprint</code>. In
addition, there you have an option to sort the keys, which should help your
comparison</p>

<pre><code>    &gt;&gt;&gt; import json

    &gt;&gt;&gt; d1 = {'a': 'a', 'b': 'b', 'c':'c'}
    &gt;&gt;&gt; print(json.dumps(d1, sort_keys=True, indent=''))
    {
    "a": "a",
    "b": "b",
    "c": "c"
    }
    &gt;&gt;&gt; d2 = {'a': 'a ' * 50, 'b':'b', 'c': 'c ' * 50}
    &gt;&gt;&gt; print(json.dumps(d2, sort_keys=True, indent=''))
    {
    "a": "a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a ",
    "b": "b",
    "c": "c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c c "
    }
</code></pre>

python dictionary pretty-print