Lorenzo Castagno
Lorenzo Castagno

Reputation: 572

Get keys with maximum value from a nested dictionary

Thanks in advance for your help.

I built the following code (I tried the below, I used a dictionary within a dictionary).

import operator
character = {'eyes_color':{"blue":10,"brown":12},
         'hair_color':{"black":15, "blonde":7},
         'gender': {"male":16,"female":6}
         }
maximun_key=max(character.items(), key=operator.itemgetter(1))[0]

As you can see, I used in my code:

maximun_key=max(character.items(), key=operator.itemgetter(1))[0]

Getting as output:

brown

male

black

i.e. the maximum, but for each dictionary.

I expected for this case an output like:

male

I mean the keys with maximum values.

Does anyone know how I can solve this problem?

Upvotes: 3

Views: 2698

Answers (4)

Jab
Jab

Reputation: 27485

You can do this using map and functools.partial as well.

vmax = partial(max, key=itemgetter(1))
vmax(map(vmax, map(dict.items, character.values())))[0]

This uses partial to make a reusable max function with a custom key, then just map dict.items to each sub dict, and the max to that then just get the max of those results.

>>> from operator import itemgetter
>>> from functools import partial
>>> character = {'eyes_color':{"blue":10,"brown":12}, 'hair_color':{"black":15, "blonde":7}, 'gender': {"male":16,"female":6}}
>>> vmax = partial(max, key=itemgetter(1))
>>> max(map(vmax, map(dict.items, character.values())))[0]
male 

Upvotes: 2

Anupam Bera
Anupam Bera

Reputation: 519

An alternative way -

Using lambda:

character = {'eyes_color':{"blue":10,"brown":12},
         'hair_color':{"black":15, "blonde":7},
         'gender': {"male":16,"female":6}
         }

maximun_key= max([max(chars.items(),key = lambda x: x[1]) for chars in character.values()],key = lambda x: x[1])[0]

OR

Using operator.itemgetter :

from operator import itemgetter
character = {'eyes_color':{"blue":10,"brown":12},
         'hair_color':{"black":15, "blonde":7},
         'gender': {"male":16,"female":6}
         }
maximun_key= max([max(chars.items(),key=itemgetter(1)) for chars in character.values()],key=itemgetter(1))[0]

Upvotes: 2

Rahul charan
Rahul charan

Reputation: 837

Use the below code to get the result:-

import numpy as np
key_list, value_list = [], []
character = {'eyes_color':{"blue":10,"brown":12},
     'hair_color':{"black":15, "blonde":7},
     'gender': {"male":16,"female":6}
     }

for var in character.values():
    for i in var :
        key_list.append(i)
        value_list.append(var[i])

max_index = np.argmax(value_list)   #Getting index of maximum value
result = key_list[max_index]  #Getting result with the help of index value.
print(result)

Output

male

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

In simple way with builtin features:

d = {'eyes_color': {"blue": 10, "brown": 12},
     'hair_color': {"black": 15, "blonde": 7},
     'gender': {"male": 16, "female": 6}
     }

max_value, max_key = max(((v,k) for inner_d in d.values() for k,v in inner_d.items()))
print(max_key)   # male
print(max_value) # 16

Upvotes: 3

Related Questions