1

I know how to find the key corresponding to the maximum value in a dictionary, thanks to the answers to the following quesyions on Stackoverflow -

Print the key of the max value in a dictionary the pythonic way,

key corresponding to maximum value in python dictionary,

Getting key with maximum value in dictionary?, etc.

But I am not being able to understand how these will work out for a dictionary of dictionary.

Example-
I have a dictionary of dictionary d[x][l]. Suppose, I need to find the following- For a particular l='green', I need to find the corresponding value of x for which d[x]['green'] is maximum.

How to use the max() function in this case? I want to avoid looping over. I was hoping to find something equivalent to the MATLAB way of doing it in a matrix- max(d(:,l)).

d[x][l] takes integer values, and so does x.

2 Answers 2

3

Use a lambda:

max(d, key=lambda x: d[x]['green'])

The key function is called with each key in d; if you want to find the key for which d[key]['green'] is highest, you return exactly that.

Demo:

>>> d = {10: {'green': 42}, 81: {'green': 5, 'blue': 100}}
>>> max(d, key=lambda x: d[x]['green'])
10

d[10]['green'] is the highest value, so 10 is returned.

12
  • Thanks for the answer. I am still learning how the lambda operator works! Feb 20, 2014 at 9:38
  • What will the answer be, if x takes on values from a list, instead of all the keys in the dictionary? Feb 20, 2014 at 9:39
  • 1
    @SatarupaGuha: lambda simply creates a function, with one expression that whose outcome is returned. lambda x: d[x]['green'] is the same as def somefunction(x): return d[x]['green'] except you don't have to give it a name.
    – Martijn Pieters
    Feb 20, 2014 at 9:39
  • @SatarupaGuha: d is a dictionary, looping over the dictionary (as max() will do) yields keys, so x is one key in d for each call. d[x] is then one of the values in d, which according to your question are all dictionaries with at least a key 'green' in it.
    – Martijn Pieters
    Feb 20, 2014 at 9:41
  • 1
    @ifreak: just remember that the key function is called for each value in the iterable. As long as that function returns a value over which to calculate the maximum it'll work.
    – Martijn Pieters
    Apr 7, 2014 at 14:51
0
d = {10: 100, 81:500}
s=max(d,key=d.get)
print(s)
3
  • 1
    Hi, can you please explain or link relevant docs to understand how does your answer work?
    – aaossa
    Mar 3, 2022 at 10:59
  • See "Explaining entirely code-based answers". While this might be technically correct, it doesn't explain why it solves the problem or should be the selected answer. We should educate along with helping solve the problem. Mar 4, 2022 at 21:16
  • @aaossa thank u for your opinion from next tilme i will ensure it ... Mar 5, 2022 at 6:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.