Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to properly pretty-print OrderedDict?

I like the pprint module in Python. I use it a lot for testing and debugging. I frequently use the width option to make sure the output fits nicely within my terminal window.

It has worked fine until they added the new ordered dictionary type in Python 2.7 (another cool feature I really like). If I try to pretty-print an ordered dictionary, it doesn't show nicely. Instead of having each key-value pair on its own line, the whole thing shows up on one long line, which wraps many times and is hard to read:

>>> from collections import OrderedDict >>> o = OrderedDict([("aaaaa", 1), ("bbbbbb", 2), ("ccccccc", 3), ("dddddd", 4), ("eeeeee", 5), ("ffffff", 6), ("ggggggg", 7)]) >>> import pprint >>> pprint.pprint(o) OrderedDict([('aaaaa', 1), ('bbbbbb', 2), ('ccccccc', 3), ('dddddd', 4), ('eeeeee', 5), ('ffffff', 6), ('ggggggg', 7)]) 

Does anyone here have a way to make it print nicely, like the old unordered dictionaries? I could probably figure something out, possibly using the PrettyPrinter.format method, if I spend enough time, but I am wondering if anyone here already knows of a solution.

UPDATE: I filed a bug report for this. You can see it at http://bugs.python.org/issue10592.

like image 581
Elias Zamaria Avatar asked Nov 29 '10 05:11

Elias Zamaria


People also ask

How do I print a Pprint dictionary?

Dictionaries and lists can be converted to the string with str() . In this case, they are converted to a one-line string without newline, as in the output of print() . You can use pprint. pformat() to get the output of pprint.

How do you print an entire dictionary in Python?

Python's dict. values() method can be used to retrieve the dictionary values, which can then be printed using the print() function.


2 Answers

Ever since Python 3.7, Python guarantees that keys in a dictionary will retain their insertion order. (They still don't behave exactly the same as OrderedDict objects, though, as two dicts a and b can be considered equal a == b even if the order of the keys is different, whereas OrderedDict does check this upon testing for equality.)

Python 3.8 or newer:

You can use sort_dicts=False to prevent it from sorting them alphabetically:

>>> example_dict = {'x': 1, 'b': 2, 'm': 3} >>> import pprint >>> pprint.pprint(example_dict, sort_dicts=False) {'x': 1, 'b': 2, 'm': 3} 

Python 3.7 or older:

As a temporary workaround, you can try dumping in JSON format instead of using pprint.

You lose some type information, but it looks nice and keeps the order.

>>> import json >>> print(json.dumps(example_dict, indent=4)) {     "x": 1,     "b": 2,     "m": 3 } 
like image 85
webwurst Avatar answered Sep 19 '22 13:09

webwurst


The following will work if the order of your OrderedDict is an alpha sort, since pprint will sort a dict before print.

>>> from collections import OrderedDict >>> o = OrderedDict([("aaaaa", 1), ("bbbbbb", 2), ("ccccccc", 3), ("dddddd", 4), ("eeeeee", 5), ("ffffff", 6), ("ggggggg", 7)]) >>> import pprint >>> pprint.pprint(dict(o.items())) {'aaaaa': 1,  'bbbbbb': 2,  'ccccccc': 3,  'dddddd': 4,  'eeeeee': 5,  'ffffff': 6,  'ggggggg': 7}  

Ever since Python 3.7, Python guarantees that keys in a dictionary will retain their insertion order. So if you are using Python 3.7+, you don't need to make sure that your OrderedDict is alphabetically sorted.

like image 29
kzh Avatar answered Sep 20 '22 13:09

kzh