I have a list of dictionaries. Is it possible to get the dictionary or its index that has the highest score key value? Here is the list:
lst = [{'name': 'tom', 'score': 5},
{'name': 'jerry', 'score': 10},
{'name': 'jason', 'score': 8}]
It should return:
{'name': 'jerry', 'score': 10}
An alternative to using a lambda
for the key
argument to max
, is operator.itemgetter
:
from operator import itemgetter
max(lst, key=itemgetter('score'))
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