I have a dict {'a': 2, 'b': 0, 'c': 1}
.
Need to sort keys by values so that I can get a list ['b', 'c', 'a']
Is there any easy way to do this?
sorted_keys = sorted(my_dict, key=my_dict.get)
>>> d={'a': 2, 'b': 0, 'c': 1}
>>> [i[0] for i in sorted(d.items(), key=lambda x:x[1])]
['b', 'c', 'a']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With