Stop Using print() Every Time In Python — Use pprint() Instead

<h1>Enter our benevolent saviour pprint()</h1> <p><code>pprint</code>&nbsp;is built-in, so we don&rsquo;t need to install anything.</p> <pre> from pprint import pprint pprint(x)</pre> <ul> <li>one simple&nbsp;<code>from pprint import pprint</code>&nbsp;line is needed</li> <li>it makes our messy data more human-readable</li> <li>it doesn&rsquo;t require us to manually make it human-readable</li> </ul> <h1>Can&rsquo;t we just write a for loop instead?</h1> <p>Let&rsquo;s say we have a even more messy data structure.</p> <pre> x = { &#39;name&#39;:&#39;tom&#39;, &#39;dad&#39;: {&#39;name&#39;: &#39;jerry&#39;, &#39;dad&#39;:{&#39;name&#39;:&#39;greg&#39;}, &#39;mom&#39;:{&#39;name&#39;:&#39;susie&#39;} }, &#39;mom&#39;: {&#39;name&#39;: &#39;mary&#39;}, &#39;wife&#39;: {&#39;name&#39;: &#39;susan&#39;}, &#39;son&#39;: {&#39;name&#39;: &#39;tim&#39;, &#39;dad&#39;:{&#39;name&#39;:&#39;tom&#39;}, &#39;mom&#39;:{&#39;name&#39;:&#39;susan&#39;}, &#39;wife&#39;:{&#39;name&#39;:&#39;cassie&#39;}, &#39;daughter&#39;: {&#39;name&#39;:&#39;lala&#39;, &#39;husband&#39;: &#39;bobo&#39; } } }</pre> <p>Here we have some screwed up family tree as an example with multiple multiple levels of nesting.</p> <p>We have 2 options to visualize this:</p> <ol> <li>we manually write a recursive function to print out everything</li> <li>we use&nbsp;<code>pprint</code></li> </ol> <p><a href="https://levelup.gitconnected.com/stop-using-print-every-time-in-python-use-pprint-instead-e8761b25048f"><strong>Read More</strong></a></p>
Tags: Python pprint