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

91,748

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 ints):

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))
Share:
91,748
Hank Fay
Author by

Hank Fay

Updated on July 08, 2022

Comments

  • Hank Fay
    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() and max() 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
    dcrosta about 13 years
    nitpick: those are generator expressions. List comprehensions are surrounded by [ and ], and actually generate a Python list as an intermediate step.
  • rlibby
    rlibby about 13 years
    @dcrosta, yes, thank you, you're right of course. I changed the wording since that was embarrassing.
  • Hank Fay
    Hank Fay about 13 years
    I 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
    Hank Fay about 13 years
    Ah, that's a nice touch, returning the entire item. Not needed in this instance, but very definitely a keeper for the future.
  • svenwildermann
    svenwildermann almost 10 years
    that's what i was looking for. Awesome. Thanks!
  • Charles L.
    Charles L. over 7 years
    If 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
    anapaulagomes over 7 years
    An elegant solution!
  • thomas.mac
    thomas.mac over 6 years
    How would you do this to find the largest 5 items in a list? (not just the max)
  • hibernado
    hibernado over 6 years
    @thomas.mac You could sort and then select the top 5? see stackoverflow.com/questions/72899/…
  • Suncatcher
    Suncatcher about 5 years
    It throws AttributeError: module 'sys' has no attribute 'maxint'
  • Kuzon
    Kuzon about 5 years
    @Hugh Bothwell This is magical! Can you help me find the resources to explain this? Thanks!
  • Romain
    Romain over 4 years
    This 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
    Daniel Lavedonio de Lima over 3 years
    @Suncatcher in Python 3, sys.maxint has changed to sys.maxsize
  • Kulu
    Kulu almost 3 years
    How would you make sure that ['price'] exists in the dict?