I am working with large nested dictionaries in Python and would like to quickly check their structure in IDLE. The pprint
module nicely displays this structure, but leads IDLE to hang due to the size the variables. Is there a simple way to limit the number of lines that pprint
prints, or similarly limit the number of key-value pairs displayed at each level of structure? (I may be missing something obvious here.)
Of note, there appears to be a reprlib module made for the similar goal of truncating the output of print
after a certain number of characters. However, print
does not display the structure of large nested dictionaries in a readable fashion, so this module seems to be impractical for my purpose.
You could format the data to a string, split it into lines, and print only as many as desired:
for line in pprint.pformat(data).splitlines()[:42]:
print(line)
Instead of slicing, you could also just print all lines and stop with Ctrl-C when you've seen enough.