Home > OS >  Merge 2 dictionaries, and try to update the sum if there is common keys?
Merge 2 dictionaries, and try to update the sum if there is common keys?

Time:07-03

I have two dictionaries, and I need to combine them. But if a key is in both dictionaries, the new value must be the sum of the previous two. That is what I tried:

dict1 = {
    "Elizabeth Alexandra Mary": 250000,
    "Barack Hussein Obama II": 1750000,
    "Zhang Aiqin": 1000,
    "Dean Craig Pelton": 1000000,
    }

dict2 = {
    "Christopher Larkin": 50000,
    "Eyal Shani": 5000,
    "Dean Craig Pelton": 2500000,
    "Sheldon Cooper": 15600000
    }

dict1.update(dict2)
print('Updated dictionary:')
print(dict1)

But the value of Dean Craig Pelton is 2500000 and not 3500000 as I wanted. How can I fix this?

CodePudding user response:

Other suggestions will work. But if you want to try this more Pythonic way: (or just being lazy ... ;-)

from collections import Counter

d3 = Counter(dict1)   Counter(dict2)

print(d3)           # to confirm it

Output:

Counter({'Sheldon Cooper': 15600000, 'Dean Craig Pelton': 3500000, 'Barack Hussein Obama II': 1750000, 'Elizabeth Alexandra Mary': 250000, 'Christopher Larkin': 50000, 'Eyal Shani': 5000, 'Zhang Aiqin': 1000})

CodePudding user response:

You could also use get() with a default of 0

newdict = {x: dict1.get(x, 0)   dict2.get(x, 0) for x in set(dict1) | set(dict2)}

CodePudding user response:

You can traverse the second dictionary checking if each key element is in the first dict, to decide whether values have to be summed or assigned:

dict1 = {
    "Elizabeth Alexandra Mary": 250000,
    "Barack Hussein Obama II": 1750000,
    "Zhang Aiqin": 1000,
    "Dean Craig Pelton": 1000000,
    }

dict2 = {
    "Christopher Larkin": 50000,
    "Eyal Shani": 5000,
    "Dean Craig Pelton": 2500000,
    "Sheldon Cooper": 15600000
    }

for i in dict2:
  if not i in dict1:
    dict1[i] = dict2[i]
  else:
    dict1[i] = dict1[i]   dict2[i]

print('Updated dictionary:')
print(dict1)

Output:

Updated dictionary:
{'Elizabeth Alexandra Mary': 250000, 'Barack Hussein Obama II': 1750000, 'Zhang Aiqin': 1000, 'Dean Craig Pelton': 3500000, 'Christopher Larkin': 50000, 'Eyal Shani': 5000, 'Sheldon Cooper': 15600000}

CodePudding user response:

A defaultdict might provide a more elegant solution than a regular dictionary. Here each value defaults to 0 if referenced before assignment.

from collections import defaultdict

dict1 = defaultdict(int, {
    "Elizabeth Alexandra Mary": 250000,
    "Barack Hussein Obama II": 1750000,
    "Zhang Aiqin": 1000,
    "Dean Craig Pelton": 1000000,
})
dict2 = {
    "Christopher Larkin": 50000,
    "Eyal Shani": 5000,
    "Dean Craig Pelton": 2500000,
    "Sheldon Cooper": 15600000
}

for k, v in dict2.items():
    dict1[k]  = v

print(dict1)

CodePudding user response:

YOu should be able to do it with a comprehension -

combined = {k: dict1.get(k, 0)   dict2.get(k, 0) for k in list(dict1.keys())   list(dict2.keys())}

Output

{'Elizabeth Alexandra Mary': 250000,
 'Barack Hussein Obama II': 1750000,
 'Zhang Aiqin': 1000,
 'Dean Craig Pelton': 3500000,
 'Christopher Larkin': 50000,
 'Eyal Shani': 5000,
 'Sheldon Cooper': 15600000}
  • Related