In this python dictionary program, we will sort items in a dictionary in descending order.
Steps to solve the program
- Take a dictionary as input.
- Sort the dictionary by values in descending order using sorted().
- Use values as the key while sorting using the lamdba function.
- Pass values of the dictionary to the lambda function.
- To sort the dictionary in descending order assign reverse equal to True.
- Print the output.
dict1 = {'Math':70,'Physics':90,'Chemistry':67}
a = sorted(dict1.items(), key = lambda val:val[1],reverse = True)
print(a)
Output :
[('Physics', 90), ('Math', 70), ('Chemistry', 67)]