0

I try to use pprint to print a list:

>>> import pprint
>>> a = [1, 3, 6, 8, 0]
>>> pprint.pprint(a)
[1, 3, 6, 8, 0]
>>> 

why not this

[1,
3,
6,
8,
0]

look forward you answer! THANKS!

Georgy
  • 12,464
  • 7
  • 65
  • 73

2 Answers2

2

According to the official documentation, it appears that you need to specifiy the width of your input which is set to 80 by default. You may try the following

>>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> pp = pprint.PrettyPrinter(indent=4, width=50)
>>> pp.pprint(stuff)
['spam', 'eggs', 'lumberjack', 'knights', 'ni']
>>> pp = pprint.PrettyPrinter(indent=4, width=1)
>>> pp.pprint(stuff)
[   'spam',
    'eggs',
    'lumberjack',
    'knights',
    'ni']
>>> 
picnix_
  • 377
  • 2
  • 7
0

you can use width parameter:

pp = pprint.PrettyPrinter(width=4)
pp.pprint(a)
Vivek Singh
  • 300
  • 1
  • 4
  • 13