1

I was wondering if there would be a way to sort a list in order according to the increasing floats as values in a dictionary. I don't really know how to explain it so I'll demonstrate what I mean

[3, 6, 4]

to

[6, 4, 3]

with dictionary like this

{6: 15.6, 3: 120.0, 4: 17.3}
Selcuk
  • 57,004
  • 12
  • 102
  • 110
biner1999
  • 35
  • 4

2 Answers2

6

Use the key argument of sorted:

>>> mylist = [3, 6, 4]
>>> mydict = {6:15.6, 3:120.0, 4:17.3}
>>> sorted(mylist, key=mydict.get)
[6, 4, 3]
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
0

For your example, you don't even need that list as the dict keys are sufficient:

>>> my_dict = {6: 15.6, 3: 120.0, 4: 17.3}
>>> sorted(my_dict, key=my_dict.get)
[6, 4, 3]
Selcuk
  • 57,004
  • 12
  • 102
  • 110