0

if input is={"i":4,"m":1,"s":4,"p":2} output should be={"i":4,"s":4,"p":2,"m":1} how i sort values in dictionary in descending order

print(sorted(dict,reverse=True)).I have used this but not getting results

Tanya Chaudhary
  • 95
  • 1
  • 4
  • 13
  • I have already provided an answer and you have accepted here https://stackoverflow.com/questions/52542867/identify-unique-letters-and-corresponding-count-in-input-string/52544046#52544046 – mad_ Sep 27 '18 at 20:34

1 Answers1

1

I cant test this code right now, but it should be fairly close

# get sorted entries
s = sorted(d.items(), key=lambda e: e[1], reverse=True)
# since Python 3.6, dictionaries are sorted by entry
# if you're using an older Python, check out collections.OrderedDict
d = {k: v for (k,v) in s}
Ben
  • 5,952
  • 4
  • 33
  • 44