Search code examples
pythonpprint

Printing a list vertically with pprint


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!


Solution

  • 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']
    >>>