Skip to content

Instantly share code, notes, and snippets.

@Keiku
Last active April 13, 2017 03:35
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 Keiku/8bd5360db2936c841d5c35e85a1fbbdd to your computer and use it in GitHub Desktop.
Save Keiku/8bd5360db2936c841d5c35e85a1fbbdd to your computer and use it in GitHub Desktop.
Get keys/values from sorted OrderedDict.
from collections import OrderedDict
d = {'A': 3,
'B': 2,
'C': 1}
OrderedDict(sorted(d.items(), key=lambda x: x[0])).values()
# Out[1]: odict_values([3, 2, 1])
OrderedDict(sorted(d.items(), key=lambda x: x[1])).values()
# Out[2]: odict_values([1, 2, 3])
OrderedDict(sorted(d.items(), key=lambda x: x[0])).keys()
# Out[3]: odict_keys(['A', 'B', 'C'])
OrderedDict(sorted(d.items(), key=lambda x: x[1])).keys()
# Out[4]: odict_keys(['C', 'B', 'A'])
# reverse
OrderedDict(sorted(d.items(), key=lambda x: x[0], reverse=True)).values()
# Out[5]: odict_values([1, 2, 3])
# get list
list(OrderedDict(sorted(d.items(), key=lambda x: x[0])).values())
# Out[6]: [3, 2, 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment