[Solved] Sorting a dictionary where values are also dictionaries?


Use x[1]["number"] in your lambda

n [40]: d
Out[40]:
{'b': {'description': 'second letter', 'number': 2},
 'a': {'description': 'first letter', 'number': 1},
 'c': {'description': 'third letter', 'number': 3}}

In [41]: sorted(d.items(), key=lambda x:x[1]["number"])
Out[41]:
[('a', {'description': 'first letter', 'number': 1}),
 ('b', {'description': 'second letter', 'number': 2}),
 ('c', {'description': 'third letter', 'number': 3})]

1

solved Sorting a dictionary where values are also dictionaries?