Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Max/Min value of Dictionary of List

Tags:

python

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 max/min of a certain index of the list mappings.

What I want to do is something like this:

maximum = max([data[0], ??) for id_, data in self.dic.items()])

...but obviously this will not work. Is it possible to do this in one line as above?

like image 822
Clev3r Avatar asked Dec 27 '22 09:12

Clev3r


1 Answers

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.

like image 152
Rohit Jain Avatar answered Jan 09 '23 14:01

Rohit Jain