I have a dictionary and I need the keys to be sorted according to their corresponding values. But the keys should not change the preset order when sorting is applied
The below example shows exactly what I am trying to do. The code returns the keys of first 3 highest values
Code
import heapq
val = {"a":3, "b":2, "c":4, "d":10, "e":6}
res = heapq.nlargest(3, val, key=val.get)
print(res)
Current Output
['d', 'e', 'c']
Expected Output
['c', 'd', 'e']
I tried this with heapq module but no use and usage of heapq module is not mandatory