1

As we know, pprint can be "annoying" when it's printing list of a lot of small words, since pprint can only accept of two modes: one-line of multiple small words, or multiple lines of small words on each line separately.

Is there some other python library which can print the dict like {"1" : [1] * 10, "2": [2]*100} in a pretty while still compact way?

Thanks!

Nan Hua
  • 3,414
  • 3
  • 17
  • 24

1 Answers1

2

Pass True as the compact argument. (Only available in Python 3.4+)

>>> pprint({"1" : [1]*10, "2": [2]*100}, compact=True)
{'1': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
 '2': [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
       2, 2, 2, 2]}
skrx
  • 19,980
  • 5
  • 34
  • 48
  • It's great. However, my working place is still using Python 2.7. Is there a way to get similar in Python 2.7? Thanks.. – Nan Hua Jul 20 '17 at 05:42
  • I usually don't use Python 2, so I've got no idea. You could try to write a function that emulates this feature of Python 3's `pprint` or convince your team to switch to Python 3. :P Python 2 support stops in two and a half years so this would actually be a good idea. – skrx Jul 20 '17 at 08:04