How to find the min/max value of a common key in a list of dicts?
Solution 1
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.
Solution 2
There are several options. Here is a straight-forward one:
seq = [x['the_key'] for x in dict_list]
min(seq)
max(seq)
[Edit]
If you only wanted to iterate through the list once, you could try this (assuming the values could be represented as int
s):
import sys
lo,hi = sys.maxint,-sys.maxint-1
for x in (item['the_key'] for item in dict_list):
lo,hi = min(x,lo),max(x,hi)
Solution 3
I think the most direct (and most Pythonic) expression would be something like:
min_price = min(item['price'] for item in items)
This avoids the overhead of sorting the list -- and, by using a generator expression, instead of a list comprehension -- actually avoids creating any lists, as well. Efficient, direct, readable... Pythonic!
Solution 4
One answer would be mapping your dicts to the value of interest inside a generator expression, and then applying the built-ins min
and max
.
myMax = max(d['price'] for d in myList)
myMin = min(d['price'] for d in myList)
Solution 5
can also use this:
from operator import itemgetter
lst = [{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}]
max(map(itemgetter('price'), lst))
Hank Fay
Updated on July 08, 2022Comments
-
Hank Fay almost 2 years
I have a list of dictionaries like so:
[{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}]
I want to find the
min()
andmax()
prices. Now, I can sort this easily enough using a key with a lambda expression (as found in another Stack Overflow post), so if there is no other way I'm not stuck. However, from what I've seen there is almost always a direct way in Python, so this is an opportunity for me to learn a bit more. -
dcrosta about 13 yearsnitpick: those are generator expressions. List comprehensions are surrounded by
[
and]
, and actually generate a Python list as an intermediate step. -
rlibby about 13 years@dcrosta, yes, thank you, you're right of course. I changed the wording since that was embarrassing.
-
Hank Fay about 13 yearsI accept this as the answer as it not only gives the answer, but it also showed me that one can abstract sequences. Darn, Python is a beautiful language. Thanks!
-
Hank Fay about 13 yearsAh, that's a nice touch, returning the entire item. Not needed in this instance, but very definitely a keeper for the future.
-
svenwildermann almost 10 yearsthat's what i was looking for. Awesome. Thanks!
-
Charles L. over 7 yearsIf you don't need the
seq
, and the list is large, this can be inefficient since the memory for the entire list has to be allocated just to find the max. -
anapaulagomes over 7 yearsAn elegant solution!
-
thomas.mac over 6 yearsHow would you do this to find the largest 5 items in a list? (not just the max)
-
hibernado over 6 years@thomas.mac You could sort and then select the top 5? see stackoverflow.com/questions/72899/…
-
Suncatcher about 5 yearsIt throws
AttributeError: module 'sys' has no attribute 'maxint'
-
Kuzon about 5 years@Hugh Bothwell This is magical! Can you help me find the resources to explain this? Thanks!
-
Romain over 4 yearsThis works perfectly. Following @thomas.mac's comment , is there an easy way to get all the minima if there are several (as a list of matching dict, for instance) ?
-
Daniel Lavedonio de Lima over 3 years@Suncatcher in Python 3,
sys.maxint
has changed tosys.maxsize
-
Kulu almost 3 yearsHow would you make sure that ['price'] exists in the dict?