145

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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Hank Fay
  • 1,704
  • 2
  • 11
  • 11

5 Answers5

374
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.

Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99
  • 12
    Ah, that's a nice touch, returning the entire item. Not needed in this instance, but very definitely a keeper for the future. – Hank Fay Mar 16 '11 at 18:29
  • How would you do this to find the largest 5 items in a list? (not just the max) – thomas.mac Oct 09 '17 at 21:17
  • 3
    @thomas.mac You could sort and then select the top 5? see https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary-in-python – hibernado Nov 03 '17 at 12:53
  • @Hugh Bothwell This is magical! Can you help me find the resources to explain this? Thanks! – Kuzon Apr 10 '19 at 07:36
  • 4
    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) ? – Romain Sep 05 '19 at 09:10
  • How would you make sure that ['price'] exists in the dict? – Kulu Jul 09 '21 at 12:54
75

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)
dappawit
  • 12,182
  • 2
  • 32
  • 26
54

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!

dcrosta
  • 26,009
  • 8
  • 71
  • 83
15

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)
rlibby
  • 5,931
  • 20
  • 25
  • nitpick: those are generator expressions. List comprehensions are surrounded by `[` and `]`, and actually generate a Python list as an intermediate step. – dcrosta Mar 16 '11 at 04:01
  • @dcrosta, yes, thank you, you're right of course. I changed the wording since that was embarrassing. – rlibby Mar 16 '11 at 04:12
5

can also use this:

from operator import itemgetter

lst = [{'price': 99, 'barcode': '2342355'}, {'price': 88, 'barcode': '2345566'}]  
max(map(itemgetter('price'), lst))
carton.swing
  • 1,517
  • 17
  • 12