How to Find the Min/Max Value of a Common Key in a List of Dicts

How to find the min/max value of a common key in a list of dicts?

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.

How to get max value from a list of dictionaries?

I provide a simple and easily understandable way using for loop, as follows:

import sys
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
}
]

max_high = ohlc[0]['high'] to assign first high value.

for i in ohlc[1:]:
if i['high'] > max_high:
max_high = i['high']

print(max_high)
#108

Find minimum key value from list of dicts, ignoring None values

Use min with generator:

myList = [{"id": 1, "qty": 100},
{"id": 2, "qty": None},
{"id": 3, "qty": -60},
{"id": 4, "qty": 120}]

print(min(d.get('qty') for d in myList if d.get('qty') is not None))

Outputs

-60

In Python >= 3.8 you can use the walrus operator so d.get('qty') is not evaluated twice:

print(min(val := d.get('qty') for d in myList if val is not None))


If you expect a situation in which all qty values will be None, you can pass default=None (or whatever value you want) to min, so it won't complain that it got an empty sequence.

Get max value index for a list of dicts

Tell max() how to calculate the maximum for a sequence of indices:

max(range(len(ld)), key=lambda index: ld[index]['size'])

This'll return the index for which the size key is the highest:

>>> ld = [{'prop': 'foo', 'size': 100}, {'prop': 'boo', 'size': 200}]
>>> max(range(len(ld)), key=lambda index: ld[index]['size'])
1
>>> ld[1]
{'size': 200, 'prop': 'boo'}

If you wanted that dictionary all along, then you could just use:

max(ld, key=lambda d: d['size'])

and to get both the index and the dictionary, you could use enumerate() here:

max(enumerate(ld), key=lambda item: item[1]['size'])

Some more demoing:

>>> max(ld, key=lambda d: d['size'])
{'size': 200, 'prop': 'boo'}
>>> max(enumerate(ld), key=lambda item: item[1]['size'])
(1, {'size': 200, 'prop': 'boo'})

The key function is passed each element in the input sequence in turn, and max() will pick the element where the return value of that key function is highest.

Using a separate list to extract all the size values then mapping that back to your original list is not very efficient (you now need to iterate over the list twice). list.index() cannot work as it has to match the whole dictionary, not just one value in it.

Python - Get max value in a list of dict

Iterate over all dictionaries in the list; iterate over each dictionary, grabbing the values. Take max of that. Use the built-in constructors to let the run-time system optimize things for you, as best it can.

In Python 2.7:

ddd = [{0: 0, 1: 1.4589, 4: 2.4879},
{0: 1.4589, 1: 0, 2: 2.1547},
{1: 2.1547, 2: 0, 3: 4.2114},
{2: 4.2114, 3: 0},
{0: 2.4879, 4: 0}]

def findMax(data):
return max(val for item in data for val in item.itervalues())

print "MAX", findMax(ddd)

Output:

MAX 4.2114

Max and min value in list of dicts where each dict key has a value

You can flatten the values, then find the min and max:

from itertools import chain

flattened = list(chain(*(d.values() for d in list_of_dicts)))
print(min(flattened))
print(max(flattened))

How to find max value in array of dictionary

You can just use max with a custom key using operator.itemgetter for fetching the number

from operator import itemgetter
max_item = max(l, key=itemgetter('number'))
print(max_item)

Outputs:

{'name': 'Chrysanthemum.jpg', 'number': 100.0}

Find maximum value in a list of dictionaries

Try this:

for dVals in yourData:
print max(dVals['items'], key=lambda x:x['age'])

Or one-liner:

print [max(dVals['items'], key=lambda x: x['age']) for dVals in yourData]

{'id': 0, 'age': 17, 'name': 'Pam1'}
{'id': 1, 'age': 8, 'name': 'Tom12'}
{'id': 2, 'age': 77, 'name': 'Pam13'}


Related Topics



Leave a reply



Submit