'How to get max value from a list of dictionaries? [duplicate]

I have python list of dictionaries as below. I would like to find max value of 'high' field.

ohlc = [
  {
    'open' : 100,
    'high' : 105,
    'low' : 95,
    'close' : 103
  },
  {
    'open' : 102,
    'high' : 108,
    'low' : 101,
    'close' : 105
  }
  {
    'open' : 101,
    'high' : 106,
    'low' : 100,
    'close' : 105
  }
]

In this case function should return high = 108.



Solution 1:[1]

Use the key parameter to the max function like so:

ohlc = [
  {
    'open' : 100,
    'high' : 105,
    'low' : 95,
    'close' : 103
  },
]
print(max(ohlc, key=(lambda item: item['high'])))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Samarth Ramesh