Skip to content

Instantly share code, notes, and snippets.

@bpluly
Created December 9, 2022 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bpluly/cbd24427f0d34a9f5246f04e66c6d9ce to your computer and use it in GitHub Desktop.
Save bpluly/cbd24427f0d34a9f5246f04e66c6d9ce to your computer and use it in GitHub Desktop.
Python 3.8 pprint
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