1

All the answers I found to this seem to make you have to specify which key you want to find the max/min for e.g. Finding minimum values in a list of dicts.

I have a list of dicts like:

 [
{
    1:34,
    2:12,
    3:24
},
{
    1:121,
    2:1211,
    3:1891
},
{
    1:191,
    2:8991,
    3:3232
}
]

So here it would pull out 12 and 8991.

And I want to find the max and min of all the values in this list of dicts.

How do I do this without having to specify each key separately (e.g. 1,2,3) - in the real version I have 20+.

Community
  • 1
  • 1
Dhruv Ghulati
  • 2,976
  • 3
  • 35
  • 51

2 Answers2

2

You can use a list comprehension or generator expression.

max([max(d.values()) for d in data])
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

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))
Karin
  • 8,404
  • 25
  • 34