how can I sort a dictionary in python according to its values?
s = {1: 1, 7: 2, 4: 2, 3: 1, 8: 1}
k = dict(sorted(s.items(),key=lambda x:x[0],reverse = True))
print(k)
how can I sort a dictionary in python according to its values?
s = {1: 1, 7: 2, 4: 2, 3: 1, 8: 1}
k = dict(sorted(s.items(),key=lambda x:x[0],reverse = True))
print(k)
python reverse dict key order
# Python3 using reversed() + items()
test_dict = {'Hello' : 4, 'to' : 2, 'you' : 5}
rev_dict = dict(reversed(list(test_dict.items()))) # Reversing the dictionary
print("The original dictionary : " + str(test_dict))
print("The reversed order dictionary : " + str(rev_dict))
# Output:
"The original dictionary : {'Hello': 4, 'to': 2, 'you': 5}"
"The reversed order dictionary : {'you': 5, 'to': 2, 'Hello': 4}"
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