'question regarding a nested dictionary is there a way to merge a nested dictionary in one dictionary

I'm following a python course on runestone and i'm stuck with the following question:

Provided is a dictionary that contains pokemon go player data, where each player reveals the amount of candy each of their pokemon have. If you pooled all the data together, which pokemon has the highest number of candy? Assign that pokemon to the variable most_common_pokemon.

what i thought is to created a dictionary that merge the common keys (and their value or to make a comparison something like

if x>y
   x=y

so I can get the pokemon with the highest number of candies

pokemon_go_data = {'bentspoon':
                  {'Rattata': 203, 'Pidgey':20, 'Drowzee': 89, 'Squirtle': 35, 'Pikachu': 3, 'Eevee': 34, 'Magikarp': 300, 'Paras': 38},
                  'Laurne':
                  {'Pidgey': 169, 'Rattata': 245, 'Squirtle': 9, 'Caterpie': 38, 'Weedle': 97, 'Pikachu': 6, 'Nidoran': 44, 'Clefairy': 15, 'Zubat': 79, 'Dratini': 4},
                  'picklejarlid':
                  {'Rattata': 32, 'Drowzee': 15, 'Nidoran': 4, 'Bulbasaur': 3, 'Pidgey': 56, 'Weedle': 21, 'Oddish': 18, 'Magmar': 6, 'Spearow': 14},
                  'professoroak':
                  {'Charmander': 11, 'Ponyta': 9, 'Rattata': 107, 'Belsprout': 29, 'Seel': 19, 'Pidgey': 93, 'Shellder': 43, 'Drowzee': 245, 'Tauros': 18, 'Lapras': 18}}

pokemon=[]

for i,k in pokemon_go_data.items():
    b=k.keys()
    b=list(b)
    pokemon.append(b)
print (pokemon)  

poke=[]

for i in pokemon:
    for j in i:
        if j not  in poke:
            poke.append(j)
        else:
            continue
print(poke) 

d={}
n=0
count=[]
total=0
most_common_pokemon=""

for players in pokemon_go_data:
    for pokemon in pokemon_go_data[players]:
            if pokemon==poke[n]:
                 count.append(pokemon_go_data[players][pokemon])
                 counts=sum(count)
                 print (count)
                 print(counts)
                 d[poke[n]]=counts
print (d) 

by doing so it prints a dictionary: {'Rattata': 587}

but if i add a counter like n+=1 i got the following

{'Rattata': 203, 'Pidgey': 372, 'Drowzee': 387}

if instead of creating a dictionary something like

if count>total:
            total=count
            most_common_pokemon=poke[n]
n+1=n

i got a out of range error message i placed the counter everywhere but it doesn't work...and also when i reset count

thanks any suggestion is more than welcome



Solution 1:[1]

This should do it:

pokemon_total = {}

for player, dictionary in pokemon_go_data.items():
    for pokemon, candy_count in dictionary.items():
        if pokemon in pokemon_total.keys():
            pokemon_total[pokemon] += candy_count
        else:
            pokemon_total[pokemon] = candy_count

most_common_pokemon = max(pokemon_total, key=pokemon_total.get)

print(most_common_pokemon)

Solution 2:[2]

T did this just now worked for me and gives the correct answer 'Rattata'

pokemon_go_data = {'bentspoon':
                       {'Rattata': 203, 'Pidgey': 120, 'Drowzee': 89, 'Squirtle': 35, 'Pikachu': 3, 'Eevee': 34,
                        'Magikarp': 300, 'Paras': 38},
                   'Laurne':
                       {'Pidgey': 169, 'Rattata': 245, 'Squirtle': 9, 'Caterpie': 38, 'Weedle': 97, 'Pikachu': 6,
                        'Nidoran': 44, 'Clefairy': 15, 'Zubat': 79, 'Dratini': 4},
                   'picklejarlid':
                       {'Rattata': 32, 'Drowzee': 15, 'Nidoran': 4, 'Bulbasaur': 3, 'Pidgey': 56, 'Weedle': 21,
                        'Oddish': 18, 'Magmar': 6, 'Spearow': 14},
                   'professoroak':
                       {'Charmander': 11, 'Ponyta': 9, 'Rattata': 107, 'Belsprout': 29, 'Seel': 19, 'Pidgey': 93,
                        'Shellder': 43, 'Drowzee': 245, 'Tauros': 18, 'Lapras': 18}}

count_d={}
pokemon_main_lst=pokemon_go_data.keys()
#print(pokemon_main_lst)
for main_keys in pokemon_main_lst:
    pokemon_sub_lst=pokemon_go_data[main_keys].keys()
    #print(sub_lst)
    for pokemon in pokemon_sub_lst:
        if pokemon not in count_d:
            count_d[pokemon]=0
        count_d[pokemon]+=pokemon_go_data[main_keys][pokemon]
#print(count_d)

most_common_pokemon=sorted(count_d,key=lambda k:count_d[k])[-1]
print(most_common_pokemon)

Solution 3:[3]

out = {}

for k,v in [[k2,p[k1][k2]] for k1 in p for k2 in p[k1]]:
    if k in out.keys():
        out[k] = out[k] + v
    else:
        out[k] = v

print(max(out, key=out.get))

basically same as above answer in principle but slightly diff implementation

OR

from itertools import groupby    
out = sorted([[k2,p[k1][k2]] for k1 in p for k2 in p[k1]])
result = {a:sum(c for _, c in b) for a, b in groupby(out, key=lambda x:x[0])}
print(max(result,key=result.get))

OR

out = sum(map(Counter, p.values()), Counter())
print(max(out,key=result.get))

Solution 4:[4]

pokemon_total = {}

for player, dictionary in pokemon_go_data.items():
    for pokemon, candy_count in dictionary.items():
        if pokemon in pokemon_total.keys():
            pokemon_total[pokemon] += candy_count
        else:
            pokemon_total[pokemon] = candy_count

max_occurence = 0
for pokemon in pokemon_total:
    if pokemon_total[pokemon] > max_occurence :
        max_occurence = pokemon_total[pokemon]
        most_common_pokemon = pokemon    
print(most_common_pokemon)

Solution 5:[5]

This may help

d={}
for name,val in pokemon_go_data.items():
    for key in val:
        if key not in d:
            d[key]=0
        d[key]+=pokemon_go_data[name][key]
most_common_pokemon=sorted(d.keys(), key= lambda k:d[k])[-1]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Clade
Solution 2
Solution 3
Solution 4 30Coder
Solution 5 Syed Abdur Rafay