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

location of member in list of dictionaries

if I have the following:

[{'place':1, 'acc':75}, {'place':2, 'acc':95}, {'place':3, 'acc':60}]

What would be the best way to get the value of the place which has the best accuracy ?
In this example the result would be: 2

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 :

The built-in max function allows you to specify a key. So, for your example, you could do this:

list_ = [{'place':1, 'acc':75}, {'place':2, 'acc':95}, {'place':3, 'acc':60}]

print(max(list_, key=lambda x: x['acc'])['place'])

Output:

2
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