Sort a dictionary containing child dictionary, by using value of a key in the child dictionary

Viewed 151

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?

1 Answers

You can try something like this

dict(sorted(a_dict.items(), key=lambda item: item[1]["height"]))
Related