Skip to content Skip to sidebar Skip to footer

Max/min Value Of Dictionary Of List

I have a dictionary mapping an id_ to a list of data values like so: dic = {id_ : [v1, v2, v3, v4]}. I'm trying to iterate through every value in the dictionary and retrieve the ma

Solution 1:

You need to use it something like this:

maximum = max(data[0] for data in dic.values())

since you are not using your keys, simply use dict.values() to get just the values.

Solution 2:

Using a generator expression and max():

In [10]: index = 0

In [11]: dictA = { 1 : [22, 31, 14], 2 : [9, 4, 3], 3 : [77, 15, 23]}

In [12]: max(l[index] for l in dictA.itervalues())
Out[12]: 77

Note: itervalues() returns an iterator over the dictionary’s values without making a copy, and is therefore more efficient than values() (in Python < 3).

Post a Comment for "Max/min Value Of Dictionary Of List"