Home > database >  How to find the max value from a list of dicts based on different keys in Python
How to find the max value from a list of dicts based on different keys in Python

Time:02-21

I have below list of dicts

[{
    'NAV': 50,
    'id': '61e6b2a1d0c32b744d3e3b2d'
}, {
    'NAV': 25,
    'id': '61e7fbe2d0c32b744d3e6ab4'
}, {
    'NAV': 30,
    'id': '61e801cbd0c32b744d3e7003'
}, {
    'NAV': 30,
    'id': '61e80663d0c32b744d3e7c51'
}, {
    'NAV': 30,
    'id': '61e80d9ad0c32b744d3e8da6'
}, {
    'NAV': 30,
    'id': '61e80f5fd0c32b744d3e93f0'
}, {
    'NAV': 30,
    'id': '61e90908d0c32b744d3ea967'
}, {
    'NAV': 30,
    'id': '61ea7cf3d0c32b744d3ed1b2'
}, {
    'NAV': 50,
    'id': '61fa387127e14670f3a67194'
}, {
    'NAV': 30,
    'id': '61fa3cea27e14670f3a6772c'
}, {
    'Amount': 30,
    'id': '61e6b373d0c32b744d3e3d14'
}, {
    'Amount': 30,
    'id': '61e6b49cd0c32b744d3e3ea0'
}, {
    'Amount': 25,
    'id': '61e7fe90d0c32b744d3e6ccd'
}, {
    'Amount': 20,
    'id': '61e80246d0c32b744d3e7242'
}, {
    'Amount': 20,
    'id': '61e80287d0c32b744d3e74ae'
}, {
    'Amount': 20,
    'id': '61e80253d0c32b744d3e733e'
}, {
    'Amount': 34,
    'id': '61e80697d0c32b744d3e7edd'
}, {
    'Amount': 20,
    'id': '61e806a3d0c32b744d3e7ff9'
}, {
    'Amount': 30,
    'id': '61e80e0ad0c32b744d3e906e'
}, {
    'Amount': 30,
    'id': '61e80e22d0c32b744d3e9198'
}, {
    'Amount': 20,
    'id': '61e81011d0c32b744d3e978e'
}, {
    'Amount': 20,
    'id': '61e8104bd0c32b744d3e9a92'
}, {
    'Amount': 20,
    'id': '61e81024d0c32b744d3e98cd'
}, {
    'Amount': 20,
    'id': '61e90994d0c32b744d3eac2b'
}, {
    'Amount': 20,
    'id': '61e909aad0c32b744d3ead76'
}, {
    'Amount': 50,
    'id': '61fa392a27e14670f3a67337'
}, {
    'Amount': 50,
    'id': '61fa393727e14670f3a67347'
}, {
    'Amount': 50,
    'id': '61fa3d6727e14670f3a67750'
}, {
    'Amount': 150,
    'id': '61fa3d7127e14670f3a67760'
}]

Above list contains dict which has key as NAV and Amount. I need to find the max value separately among all the dicts for NAV and Amount. So that output is

NAV = 50
Amount = 150

I have tried some approach like:

max(outList, key=lambda x: x['NAV'])

But this is giving me keyerror of 'NAV'. What is the best way to do it?

CodePudding user response:

I don't understand why you are calling Current NAV ($ M). It doesn't exist in the list you have provided. Anyway, I came up with the code below:

def getMax(value):
  if "NAV" in value:
    return value["NAV"]
  else:
    return value["Amount"]
  
max(outList, key= getMax)

If you are interested in finding the max value for NAV and Amount separately, you can try filtering the list out and then calling the lambda as you used before.

print(max([x["NAV"] for x in outList if "NAV" in x]))
print(max([x["Amount"] for x in outList if "Amount" in x]) 

CodePudding user response:

you could try something like this:

print max([i["NAV"] for i in t if "NAV" in i])
print max([i["Amount"] for i in t if "Amount" in i])

Result:

50
150
def max_from_list(t_list, key):
    return max([i[key] for i in t_list if key in i])

CodePudding user response:

You are on the right track with your max solution:

assuming your list is called outList (BTW, that is not a pythonic name, try out_list instead)

nav = max(outList, key=lambda item: item.get("NAV", float("-inf")))['NAV']
amount = max(outList, key=lambda item: item.get("Amount", float("-inf")))['Amount']

CodePudding user response:

if you are not only looking for NAV and Amount, maybe you can do as below:

from collections import defaultdict
res = defaultdict(list)

for i in d:
    for k, v in i.items():
        if k != 'id':
            res[k] = max(res.get(k, 0), v)
  • Related