I'm trying to get a pretty print of a dictionary, but I'm having no luck:
>>> import pprint >>> a = {'first': 123, 'second': 456, 'third': {1:1, 2:2}} >>> pprint.pprint(a) {'first': 123, 'second': 456, 'third': {1: 1, 2: 2}}
I wanted the output to be on multiple lines, something like this:
{'first': 123, 'second': 456, 'third': {1: 1, 2: 2} }
Can pprint
do this? If not, then which module does it? I'm using Python 2.7.3.
To print Dictionary values, use a for loop to traverse through the dictionary values using dict. values() iterator, and call print() function. In the following program, we shall initialize a dictionary and print the dictionary's values using a Python For Loop.
The purpose is very simple, it is for printing anything in python. pprint() function also has similar functionality. But the only difference is in the way it prints complex data structures. The normal print() function prints the entire content in a single line.
Use width=1
or width=-1
:
In [33]: pprint.pprint(a, width=1) {'first': 123, 'second': 456, 'third': {1: 1, 2: 2}}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With