Sort items in a dictionary in descending order.

In this python dictionary program, we will sort items in a dictionary in descending order.

Steps to solve the program
  1. Take a dictionary as input.
  2. Sort the dictionary by values in descending order using sorted().
  3. Use values as the key while sorting using the lamdba function.
  4. Pass values of the dictionary to the lambda function.
  5. To sort the dictionary in descending order assign reverse equal to True.
  6. 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)]

				
			

count a number of items in a dictionary value that is in a list.

replace dictionary values with their average.

Leave a Comment