python - sort dictionary by value
d = {'one':1,'three':3,'five':5,'two':2,'four':4}
# Sort
a = sorted(d.items(), key=lambda x: x[1])
# Reverse sort
r = sorted(d.items(), key=lambda x: x[1], reverse=True)
python - sort dictionary by value
d = {'one':1,'three':3,'five':5,'two':2,'four':4}
# Sort
a = sorted(d.items(), key=lambda x: x[1])
# Reverse sort
r = sorted(d.items(), key=lambda x: x[1], reverse=True)
sort dict by value python 3
>>> d = {"aa": 3, "bb": 4, "cc": 2, "dd": 1}
>>> for k in sorted(d, key=d.get, reverse=True):
... k, d[k]
...
('bb', 4)
('aa', 3)
('cc', 2)
('dd', 1)
sort a dictionary by value then key
d = {'apple': 2, 'banana': 3, 'almond':2 , 'beetroot': 3, 'peach': 4}
[v[0] for v in sorted(d.items(), key=lambda kv: (-kv[1], kv[0]))]
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us