Search code examples
pythonpretty-print

PrettyPrinter - Print output on separate lines from the start and end bracket


Using something like:

pp = pprint.PrettyPrinter(indent=4, width=...).pprint

Current output of pp(my_list):

[   1,
    2,
    3]

Desired output:

[
    1,
    2,
    3
]

How might this be done?


Solution

  • Using json module.

    Ex:

    import json
    my_list = [1,2,3]
    print(json.dumps(my_list, indent=4))
    

    Output:

    [
        1, 
        2, 
        3
    ]