I am trying to write a function that add all of the the inner key and value pairs of a nested dictionary.
This is what I would pass in
Pets = {'family1':{'dogs':2,'cats':3,'fish':1},
'family2':{'dogs':3,'cats':2}}
This is what I would expect as the result
{'dogs': 5, 'cats': 5, 'fish': 1}
This is the loop I have written so far
def addDict(d):
d2 = {}
for outKey, inKey in d.items():
for inVal in inKey:
print(inVal, " ", inKey[inVal])
d2[inVal] = inKey[inVal]
return d2
This prints
dogs 2
cats 3
fish 1
dogs 3
cats 2
and returns
{'dogs': 3, 'cats': 2, 'fish': 1}
But how can I get the data to be cumulative, because it is just giving me the data from the first dictionary.