Created
December 9, 2022 12:46
-
-
Save bpluly/cbd24427f0d34a9f5246f04e66c6d9ce to your computer and use it in GitHub Desktop.
Python 3.8 pprint
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From Python documentation for 3.8 | |
pprint | |
The pprint module added a sort_dicts parameter to several functions. By default, those functions continue to sort dictionaries before rendering or printing. However, if sort_dicts is set to false, the dictionaries retain the order that keys were inserted. This can be useful for comparison to JSON inputs during debugging. | |
In addition, there is a convenience new function, pprint.pp() that is like pprint.pprint() but with sort_dicts defaulting to False: | |
>>> | |
>>> from pprint import pprint, pp | |
>>> d = dict(source='input.txt', operation='filter', destination='output.txt') | |
>>> pp(d, width=40) # Original order | |
{'source': 'input.txt', | |
'operation': 'filter', | |
'destination': 'output.txt'} | |
>>> pprint(d, width=40) # Keys sorted alphabetically | |
{'destination': 'output.txt', | |
'operation': 'filter', | |
'source': 'input.txt'} | |
(Contributed by Rémi Lapeyre in bpo-30670.) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment