avatarLiu Zuo Lin

Summary

The article advocates for using Python's built-in pprint() function to improve the readability of complex nested data structures in place of the standard print() function.

Abstract

The article titled "Stop Using print() Every Time In Python — Use pprint() Instead" provides a mini hack to Python programming by introducing the pprint() function from the pprint module. It illustrates the difficulty of reading complex nested data structures when printed using the standard print() function, which outputs data in a single line, making it hard to decipher. In contrast, pprint() formats the data in a more readable way, with proper indentation and line breaks, without requiring additional code. The author emphasizes the ease of use, as pprint() is a built-in function that doesn't need external installation. The article also demonstrates how pprint() can handle deeply nested data, such as a family tree, and how it can be customized with parameters like indent and width to control the formatting. The author concludes by expressing a personal wish to have known about pprint() earlier in their Python journey and encourages readers to adopt this function to make their coding experience less painful.

Opinions

  • The author believes that pprint() significantly enhances the readability of complex data structures.
  • Using pprint() is preferable to manually writing a recursive function for pretty-printing nested data.
  • The author suggests that using pprint() can save time and effort, especially when dealing with multiple programming tasks.
  • Customizing pprint() with indent and width parameters is seen as a way to further improve the visual presentation of data.
  • The author implies that knowledge of pprint() could have saved them time in the past, hinting at the function's underutilization in the Python community.
  • There is an encouragement for readers to support the author by clapping for the article, subscribing to Medium through their link, or leaving comments.

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

# Mini Hack to Make Your Python Programming Less Painful

Let’s say I have a messy nested data structure.

x = [
  {'apple': [1,2,3], 'orange':[4,5,6]},
  {'pear': [7,8,9], 'pineapple':[10,11,12]},
  {'durian':[13,14,15], 'banana':[16,17,18]}
]

^ here’s a list of dictionaries containing smaller lists inside.

If we print(x), we get:

Imagine if we had more data. The output would become even more unreadable!

Enter our benevolent saviour pprint()

pprint is built-in, so we don’t need to install anything.

from pprint import pprint

pprint(x)
  • one simple from pprint import pprint line is needed
  • it makes our messy data more human-readable
  • it doesn’t require us to manually make it human-readable

Can’t we just write a for loop instead?

Let’s say we have a even more messy data structure.

x = {
  'name':'tom',
  'dad': {'name': 'jerry',
          'dad':{'name':'greg'},
          'mom':{'name':'susie'}
          },
  'mom': {'name': 'mary'},
  'wife': {'name': 'susan'},
  'son': {'name': 'tim',
          'dad':{'name':'tom'},
          'mom':{'name':'susan'},
          'wife':{'name':'cassie'},
          'daughter': {'name':'lala',
                        'husband': 'bobo'
                        }
          }
}

Here we have some screwed up family tree as an example with multiple multiple levels of nesting.

We have 2 options to visualize this:

  1. we manually write a recursive function to print out everything
  2. we use pprint
from pprint import pprint

pprint(x)

Here, pprint automatically formats our messy as hell data structure with zero effort needed from our part.

Note — if you wish to challenge yourself and write a recursive function to do this, by all means. When I have 100 other things to care about, I’ll take pprint in a heartbeat!

pprint() but with more indent

Sometimes we want more indent to make things neater. We can do this by adding the indent keyword inside pprint

from pprint import pprint

pprint(x, indent=4)

^ neater I guess? this is up to your preference

pprint() but with a max width

Sometimes even with pprint, our output still looks messy as hell. In these cases, we can set a max width inside the pprint function — every line printed will not exceed this max width.

from pprint import pprint

pprint(x, indent=4, width=30)

^ slightly neater!

Conclusion

I’ve done this the manual way (without pprint) too many times, and can safely say that I wish I knew about pprint much earlier in my Python journey.

Hope this was helpful to you in some way!

Some Final words

If this story was helpful and you wish to show a little support, you could:

  1. Clap 50 times for this story (this really, really helps me out)
  2. Sign up for a Medium membership using my link ($5/month to read unlimited Medium stories)
  3. Leave a comment telling me what you think!

My Ebooks: https://zlliu.co/ebooks

My LinkedIn: https://www.linkedin.com/in/zlliu/

Python
Programming
Python Programming
Software Engineering
Tech
Recommended from ReadMedium