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.

Community
  • 1
  • 1
Satarupa Guha
  • 1,267
  • 13
  • 20

2 Answers2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks for the answer. I am still learning how the lambda operator works! – Satarupa Guha Feb 20 '14 at 09:38
  • What will the answer be, if x takes on values from a list, instead of all the keys in the dictionary? – Satarupa Guha Feb 20 '14 at 09: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 '14 at 09: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 '14 at 09:41
  • I want to loop over a selected list of keys, and not all the keys. For example- maxlNeighbor=0 for item in g.neighbors(u): if degree[item][l]>maxlNeighbor: maxlNeighbor=degree[item][l] v=item – Satarupa Guha Feb 20 '14 at 09:46
  • @SatarupaGuha: then pass that list to `max()`; the key will still be the same, provided the list is a subset of all keys in `d`. – Martijn Pieters Feb 20 '14 at 09:47
  • @SatarupaGuha: so `maxlNeighbor = max(g.neighbors(u), lambda x: d[x][l])`, using the same `g`, `u` and `l` as the code in your comment. – Martijn Pieters Feb 20 '14 at 09:51
  • @MartijnPieters how can i change the statement that finds the max if i want to find the max of something like: `d['green'][key]` – ifreak Apr 07 '14 at 14:48
  • 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 07 '14 at 14:51
  • @MartijnPieters assuming the following dict : `d = {'A':{'B':20,'C':50},'AA':{'B':10,'C':70}}` using : `max(d, key=lambda x: d['A'][x])` does not work, why? – ifreak Apr 07 '14 at 14:57
  • @MartijnPieters I think what i should do is : `max(d['A'], key=lambda x: d['A'][x])` right ? – ifreak Apr 07 '14 at 15:00
  • @ifreak: exactly, because then you are looping over the keys of `d['A']`. – Martijn Pieters Apr 07 '14 at 15:07
0
d = {10: 100, 81:500}
s=max(d,key=d.get)
print(s)
  • 1
    Hi, can you please explain or link relevant docs to understand how does your answer work? – aaossa Mar 03 '22 at 10:59
  • See "[Explaining entirely code-based answers](https://meta.stackoverflow.com/q/392712/128421)". 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. – the Tin Man Mar 04 '22 at 21:16
  • @aaossa thank u for your opinion from next tilme i will ensure it ... – Sahil Panhalkar Mar 05 '22 at 06:36