How to sort a Python dict by value

Youngjae·2020년 9월 22일

Python

목록 보기
4/8
xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}
r = sorted(xs.items(), key=lambda x: x[1])
# r: [('d', 1), ('c', 2), ('b', 3), ('a', 4)]
import operator

xs = {'a': 4, 'b': 3, 'c': 2, 'd': 1}
r = sorted(xs.items(), key=operator.itemgetter(1))
# r: [('d', 1), ('c', 2), ('b', 3), ('a', 4)]

https://realpython.com

profile
Eighty percent of success is showing up.

0개의 댓글