How do I find the dictionary key for the value that maximizes some attribute or function func(value)
w/o explicit looping? For example, the longest string (when func
==len
):
d = {'x':'foo', 'a':'string', 'b':'house', 'c':'bar'}
longest = ?? # should return 'a' in this example
There is a related question asking about the maximum value, corresponding to the identity function, but it's not clear to me how to adapt the answers to that post to my use case here.