-3

I have a dictionary of ints.

d = {'jjd':2,'ddf':1,'kik':3}

Its much longer then this though. I want to sort by values highest to lowest. But I really want the results returned in an array so I can iterate through it like so:

for x in results:
    print d[x]

this should print out: ['kik','jjd','ddf']

Matt
  • 57
  • 1
  • 2
  • 9

1 Answers1

1

You can do the following collect the keys of the dicts items, sorted by descending value:

results = [k for k, v in sorted(d.items(), key=lambda i: i[1], reverse=True)]
# ['kik', 'jjd', 'ddf']
user2390182
  • 72,016
  • 6
  • 67
  • 89