How to get the max value in a nested dictionary and key that contains it?

Viewed 78

I have the following dictionary and I get the max value in every nested dictionary, but also I want to get the key that contains it. How can I get the key?

Dictionary:

{0: {'No': 0.0008701660440099657, 'Yes': 0.00041988149400856126},
 1: {'No': 0.00030532988393349125, 'Yes': 0.0004650428587258767},
 2: {'No': 0.0009029060356133256, 'Yes': 0.0007394058270439404},
 3: {'No': 5.692242884555282e-05, 'Yes': 0.0005919332682707075},
 4: {'No': 0.00015013263827995824, 'Yes': 0.00013704067699909492},
 5: {'No': 0.0006475515553117131, 'Yes': 0.000398292472360402},
 6: {'No': 0.00015299971518718442, 'Yes': 0.000636069387240845},
 7: {'No': 0.0003432474070424342, 'Yes': 0.0001701115953231753},
 8: {'No': 0.00029659442779058097, 'Yes': 0.0001243229615986186},
 9: {'No': 0.0002346339088222795, 'Yes': 4.1301441738705087e-05}}

Code that gets max value:

    for i in range(len(res_dic)):
        print(max(res_dic[i].values()))

Expected output:

No, 0.0008701660440099657
6 Answers

Try using max with key:

for i in range(len(res_dic)):
    x = max(res_dic[i], key=res_dic[i].get)
    print(x, res_dic[i][x])

Output:

No 0.0008701660440099657
Yes 0.0004650428587258767
No 0.0009029060356133256
Yes 0.0005919332682707075
No 0.00015013263827995824
No 0.0006475515553117131
Yes 0.000636069387240845
No 0.0003432474070424342
No 0.00029659442779058097
No 0.0002346339088222795

Assuming what you want is to get the max value out of all numbers in that nested dict... you can convert all of them into tuples in a generator and apply max on them:

>>> max((o[t], t, n) for n, o in res_dic.items() for t in ('Yes', 'No'))
(0.0009029060356133256, 'No', 2)

You can try this

maxi = -1000000
keyi = None

for k,v in d.items(): 
    for k1, v1 in v.items():
        if v1>maxi:
            maxi = v1
            keyi = k1
print(keyi, maxi)

Use the key parameter of max to find the maximum of the items by it's value:

from operator import itemgetter

res_dic = {0: {'No': 0.0008701660440099657, 'Yes': 0.00041988149400856126},
           1: {'No': 0.00030532988393349125, 'Yes': 0.0004650428587258767},
           2: {'No': 0.0009029060356133256, 'Yes': 0.0007394058270439404},
           3: {'No': 5.692242884555282e-05, 'Yes': 0.0005919332682707075},
           4: {'No': 0.00015013263827995824, 'Yes': 0.00013704067699909492},
           5: {'No': 0.0006475515553117131, 'Yes': 0.000398292472360402},
           6: {'No': 0.00015299971518718442, 'Yes': 0.000636069387240845},
           7: {'No': 0.0003432474070424342, 'Yes': 0.0001701115953231753},
           8: {'No': 0.00029659442779058097, 'Yes': 0.0001243229615986186},
           9: {'No': 0.0002346339088222795, 'Yes': 4.1301441738705087e-05}}

by_value = itemgetter(1)

maximum = (max(d.items(), key=by_value) for d in res_dic.values())
result = max(maximum, key=by_value)
print(result)

Output

('No', 0.0009029060356133256)

Try this one:

Val_Res_dic = res_dic.values()
for dic in Val_Res_dic:
    print(max(dic, key=dic.get), ',', max(dic.values()))

Expand the sub-dicts as key-value tuples, and use the second item as the key for the max function:

max((p for d in res_dic.values() for p in d.items()), key=lambda t: t[1])

This returns:

('No', 0.0009029060356133256)
Related