Найти тему
IT Заметки

Как отсортировать dict в Python по значению

>>> xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}

>>> {k: v for k, v in sorted(xs.items(), key=lambda item: item[1])}
# {'d': 1, 'c': 2, 'b': 3, 'a': 4}

или

>>> dict(sorted(xs.items(), key=lambda item: item[1]))