Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force pprint to print one list/tuple/dict element per line?

How can I force pprint() to print one list/tuple/dict element per line?

>>> from pprint import pprint
>>> my_var = ['one', 'two', ('red','green'), {'state' : 'Oregon', 'city' : 'Portland'}]
>>> pprint(my_var)
['one', 'two', ('red', 'green'), {'city': 'Portland', 'state': 'Oregon'}]

I would like the output to be something like:

['one',
 'two',
 ('red',
  'green'),
 {'city': 'Portland',
  'state': 'Oregon'}]
like image 290
Rob Bednark Avatar asked Mar 21 '13 14:03

Rob Bednark


People also ask

How do you print Pprint in Python?

Use pprint() in place of the regular print() Understand all the parameters you can use to customize your pretty-printed output. Get the formatted output as a string before printing it. Create a custom instance of PrettyPrinter.

How do I print a Pprint dictionary?

Use pprint() to Pretty Print a Dictionary in Python Within the pprint module there is a function with the same name pprint() , which is the function used to pretty-print the given string or object. First, declare an array of dictionaries. Afterward, pretty print it using the function pprint.

What is Pprint Pformat?

pprint() − prints the formatted representation of PrettyPrinter object. pformat() − Returns the formatted representation of object, based on parameters to the constructor. Following example demonstrates simple use of PrettyPrinter class.


1 Answers

Use a width=1 argument to pprint():

>>> from pprint import pprint
>>> my_var = ['one', 'two', ('red','green'), {'state' : 'Oregon', 'city' : 'Portland'}]
>>> pprint(my_var, width=1)
['one',
 'two',
 ('red',
  'green'),
 {'city': 'Portland',
  'state': 'Oregon'}]
>>>

"pprint - Data pretty printer" documentation

like image 165
Rob Bednark Avatar answered Sep 25 '22 00:09

Rob Bednark