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

Reorder dictonaries within a list

I have a list containing dictionaries that look like this:

[{'isin': 'IE000000001', 'MP': 'B', 'market_share': 30}, {'isin': 'IE000000002', 'MP': 'C', 'market_share': 50}, {'isin': 'IE000000003', 'MP': 'D', 'market_share': 70}]

I would like to reorder the dictionaries in my list based on their ‘market share’ so that I would end up with something like that

[{'isin': 'IE000000003', 'MP': 'D', 'market_share': 70}, {'isin': 'IE000000002', 'MP': 'C', 'market_share': 50},{'isin': 'IE000000001', 'MP': 'B', 'market_share': 30}]

Do you have any recommendations?

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

>Solution :

You don’t need pandas for this.

# sort the list by market_share
my_data.sort(key=lambda x:x['market_share'], reverse=True)
my_data
[{'isin': 'IE000000003', 'MP': 'D', 'market_share': 70},
 {'isin': 'IE000000002', 'MP': 'C', 'market_share': 50},
 {'isin': 'IE000000001', 'MP': 'B', 'market_share': 30}]
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