Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

sort list of dictionaries by the value of the dictionary

I have a list of dictionaries and want each dictionary to be sorted by value

list=[{ 1:0.2, 3:0.1, 5:0.8},{ 6:0.8, 9:0.6, 10:0.7}]

when sorted by value it should become:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[{ 3:0.1, 1:0.2, 5:0.8},{ 9:0.6, 10:0.7, 6:0.8}]

>Solution :

Starting from python 3.7 dictionaries are insertion ordered, so you could do it in the following way:

lst = [dict(sorted(d.items(), key=lambda x: x[1])) for d in lst]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading