In List of Dicts, find min() value of a common Dict field

后端 未结 5 1761
说谎
说谎 2020-11-27 12:00

I have a list of dictionaries like so:

[{\'price\': 99, \'barcode\': \'2342355\'}, {\'price\': 88, \'barcode\': \'2345566\'}]

I want to fin

5条回答
  •  旧巷少年郎
    2020-11-27 12:30

    lst = [{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}]
    
    maxPricedItem = max(lst, key=lambda x:x['price'])
    minPricedItem = min(lst, key=lambda x:x['price'])
    

    This tells you not just what the max price is but also which item is most expensive.

提交回复
热议问题