I'm running Python 2.7 in a Jupyter notebook. I am working with large nested dictionaries, and sometimes it's helpful to print out one of these.
Using pprint.pprint is a nice way of getting a readable version of the dict on screen. But for especially big dictionaries, this could mean print a million lines, which makes the notebook crash (I presume my browser is what can't handle it).
On the bash terminal I'm used to throwing things into a | head
, but there doesn't seem to be a generic way of doing that in python.
I've written this method:
from pprint import pformat, pprint
def pprint_head(to_print,length=10)
formatted=pformat(to_print).splitlines()
pprint(formatted[:min(len(formatted),length)])
It works, but I wondered
- Is there a better/more canonical/built-in/'pythonic' way to do it?
- Can any of these niggles be improved? (in order of priority):
- It's quite slow with big objects.
- It uses a lot of memory with big objects.
- It's pprinted as a list of strings so it has the [ at the beginning and quotes around each line.
I also wonder if there's a "Jupyter" solution (i.e. tell Jupyter to only accept the first x lines of any print?)