Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pprint dictionary on multiple lines

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.

like image 284
mulllhausen Avatar asked Nov 24 '13 05:11

mulllhausen


People also ask

How do you print a dictionary value?

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.

What is the difference between print and Pprint in Python?

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.


Video Answer


1 Answers

Use width=1 or width=-1:

In [33]: pprint.pprint(a, width=1) {'first': 123,  'second': 456,  'third': {1: 1,            2: 2}} 
like image 91
Warren Weckesser Avatar answered Oct 24 '22 00:10

Warren Weckesser