Home > Mobile >  Get the keys of a dictionary in the order of their values [duplicate]
Get the keys of a dictionary in the order of their values [duplicate]

Time:09-17

I think I made an overly complicated question regarding order of dictionaries, so I have simplified it to a simpler question

Let's say I have the dictionary

d1= {570.44: 2, 305.21: 1, 271.94: 0, 463.20: 3, 556.60: 4, 596.27: 5}

I want to get an ordered list of the keys but ordered according to the values not the keys

In this case I would like to get

[271.94, 305.21, 570.44, 463.20, 556.60, 596.27]

(since you can see that their values are: 0,1,2,3,4,5

CodePudding user response:

Try this:

d1= {570.44: 2, 305.21: 1, 271.94: 0, 463.20: 3, 556.60: 4, 596.27: 5}

sorted(d1, key=d1.get)

Output:

[271.94, 305.21, 570.44, 463.2, 556.6, 596.27]
  • Related