Marge Johns
Marge Johns

Reputation: 13

How to get the max value out of list

I am looking to get the highest "high" out of the dict below.

Response =

 [  
       {  
          'timestamp':'2019-04-13T04:12:00.000Z',
          'symbol':'XBTUSD',
          'open':5065,
          'high':5067,
          'low':5065.5,
          'close':5066.5,
          'trades':13,
          'volume':10002,
          'vwap':5066.8829,
          'lastSize':2,
          'turnover':197408849,
          'homeNotional':1.9740884899999998,
          'foreignNotional':10002
       },
       {  
          'timestamp':'2019-04-13T04:11:00.000Z',
          'symbol':'XBTUSD',
          'open':5065,
          'high':5065,
          'low':5065,
          'close':5065,
          'trades':0,
          'volume':0,
          'vwap':None,
          'lastSize':None,
          'turnover':0,
          'homeNotional':0,
          'foreignNotional':0
       },
       {  
          'timestamp':'2019-04-13T04:10:00.000Z',
          'symbol':'XBTUSD',
          'open':5065,
          'high':5065,
          'low':5065,
          'close':5065,
          'trades':2,
          'volume':2000,
          'vwap':5065,
          'lastSize':397,
          'turnover':39486000,
          'homeNotional':0.39486,
          'foreignNotional':2000
       }
    ]

Then to get all 'high' printed:

for h in response:
   print (h['high'])

Which prints:

5067 5065 5065

Then the question arises of how do I get the max value out of the list of numbers? It would be "5067" in this case. I have tried to use the max method, but to no avail. (max(h['high'])) does not work.

Upvotes: 1

Views: 112

Answers (3)

Madusudhanan
Madusudhanan

Reputation: 349

max(iterable, *[, key, default]) - Return the largest item in an iterable or the largest of two or more arguments.

b=max(a, key=lambda x:x['high'])
print(b['high'])

Upvotes: 2

gmds
gmds

Reputation: 19885

Use itemgetter and the key parameter:

from operator import itemgetter

max(h, key=itemgetter('high'))

Upvotes: 2

nathancy
nathancy

Reputation: 46600

You can use a list comprehension to obtain all the values from the high key then use the max() function to get the maximum

maximum = max([h['high'] for h in response])
print(maximum)

5067

Upvotes: 1

Related Questions