0

I have a dictionary in python with various numerical elements, each with a string as a key.

Example: ex_dict = {"text1" : 63 , "text2" : 71 , "text3" : 3}

I was wondering if there was a way for me to sort the elements of the dictionary based on the numerical values for each key; using a function that would output:

sorted_dict = {"text2" : 71 , "text1" : 63 , "text3" : 3}

Coolsugar
  • 131
  • 1
  • 2
  • 9

1 Answers1

1

can you try this?

ex_dict = {"text1" : 63 , "text2" : 71 , "text3" : 3}
{k: v for k, v in sorted(ex_dict.items(), key=lambda x: x[1], reverse=True)}

output

{'text2': 71, 'text1': 63, 'text3': 3}
MiH
  • 354
  • 4
  • 11