I have dictionary of dictionaries like this.
d = {
'a' : {'c' : 1, 'h' : 2},
'b' : {'c' : 3, 'h' : 5},
'c' : {'c' : 2, 'h' : 1},
'd' : {'c' : 4, 'h' : 1}
}
I need to get key of the item that has highest value of c + h
.
I know you can get key of item with highest value in case like this:
d = { 'a' : 1, 'b' : 2, 'c' : 3 }
max( d, key = d.get ) # 'c'
Is it possible to use the max
function in my case?
You can specify your own function for key
which can do fancy stuff like getting the value for both c
and h
and add those up. For example using an inline-lambda:
>>> max(d, key=lambda x: d[x]['c'] + d[x]['h'])
'b'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With