Issue 19132: Add compact mode to pprint (original) (raw)

pprint produces not very nice output for collections with a large number of short elements (see ). For example pprint.pprint(list(range(40))) outputs more than 40 short lines, while print(repr(list(range(40)))) takes only 2 lines on 80-column terminal.

I propose to add new boolean option "compact". With compact=True pprint will try combine as much short one-line subelements in one line as possible. Every multiline element will be printed on separated lines.

Examples:

pprint.pprint(list(range(40)), width=50, compact=True) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39] pprint.pprint(['one string', 'other string', 'very very long string which continued on several lines', 'and again', 'and again', 'and again', 'and again'], width=50, compact=True) ['one string', 'other string', 'very very long string which is continued on ' 'several lines', 'and again', 'and again', 'and again', 'and again']