I've been trying to make a function that adds two dictionaries together, including the integers they hold. For example:
storage = {'flour': 200, 'eggs': 4, 'rice': 450}
shoppingBag = {'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'flour': 1000, 'rice': 1000}
Would become:
{'flour': 1200, 'eggs': 10, 'rice': 1450, 'chocolate': 200, 'cream cheese', 250}
I've tried several methods:
storage = dict(storage, **shoppingBag)
#returns
{'flour': 1000, 'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'rice': 1000}
This method by Aaron Hall
def merge_two_dicts(x, y):
z = x.copy()
z.update(y)
return z
#returns
{'flour': 1000, 'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'rice': 1000}
And this one
storage = dict(list(storage.items()) + list(shoppingBag.items()))
#returns
{'flour': 1000, 'eggs': 6, 'rice': 1000, 'chocolate': 200, 'cream cheese': 250}
#which is even worse
As you can see, none of these seem to work - as far as I can gather, the two dictionaries overwrite each other
Is there a way of doing this concisely in one line, or a function?