The Max / Min value of the list dictionary
I have a dictionary that associates id_ data list of values as follows: dic = {id_ : [v1, v2, v3, v4]}
. I am trying to iterate over each value in a dictionary and get the max / min index to display the list.
I want to do something like this:
maximum = max([data[0], ??) for id_, data in self.dic.items()])
... but obviously it won't work. Can I do it in one line like above?
+3
source to share
2 answers
Using 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 values of dictionaries without making a copy and is therefore more efficient than values()
(in Python <3).
+1
source to share