Answers for "sort ditionary in reverse pyton"

35

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)
Posted by: Guest on November-23-2020
0

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}"
Posted by: Guest on June-24-2021

Python Answers by Framework

Browse Popular Code Answers by Language