Python sorting dictionary keys into a list by values -
this question has answer here:
- how sort dictionary value? 38 answers
i have dictionary say:
red : 2 blue : 1 yellow : 9 black : 9 white : 5
i need generate list of keys sorted values:
[blue, red, white, black, yellow]
is there neatish way of doing this?
i have had around, , there similar questions, seem sorting values or keys only.
you can use sorted
function dict.get
key :
sorted(your_dictionary.keys(), key=your_dictionary.get)
or says in comments can use :
sorted(your_dictionary, key=your_dictionary.get)
Comments
Post a Comment