3

Suppose that I have a dictionary containing dictionary like this example:

a_dict = {
  'abcd': {'name': 'John', 'height': 180},
  'asdf': {'name': 'Lex', 'height': 160},
  'csxf': {'name': 'Amber', 'height': 193},
}

How do I sort the dictionary using value of the key in child dictionary, i.e: value of the 'height' key?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
ewin.str
  • 109
  • 1
  • 9
  • 1
    Does this answer your question? [How do I sort a list of dictionaries by a value of the dictionary?](https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-a-value-of-the-dictionary) As well as [How do I sort a dictionary by value?](https://stackoverflow.com/q/613183/4518341) – wjandrea Jul 09 '21 at 01:59
  • No, I've been thru that and, those were not nested dictionary @wjandrea – ewin.str Jul 09 '21 at 02:20
  • 1
    It's the same idea though. Can't you see the resemblance? – wjandrea Jul 09 '21 at 02:21

1 Answers1

1

You can try something like this

dict(sorted(a_dict.items(), key=lambda item: item[1]["height"]))
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Djoby
  • 602
  • 1
  • 6
  • 22