1

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?

Oliver
  • 121
  • 6
  • 1
    Possible duplicate of [Is there any pythonic way to combine two dicts (adding values for keys that appear in both)?](https://stackoverflow.com/questions/11011756/is-there-any-pythonic-way-to-combine-two-dicts-adding-values-for-keys-that-appe) – Georgy Mar 27 '19 at 14:48
  • Not really, I don't want Counter() in the output, just a dictionary – Oliver Mar 28 '19 at 15:03
  • `Counter` is a subclass of a dictionary. If you still want to convert it back, just use `dict(your_counter)`. – Georgy Mar 28 '19 at 15:13

6 Answers6

3

Using collections.Counter

Ex:

from collections import Counter

storage = {'flour': 200, 'eggs': 4, 'rice': 450}
shoppingBag = {'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'flour': 1000, 'rice': 1000}

shoppingBag = Counter(shoppingBag) + Counter(storage)
print(shoppingBag)

Output:

Counter({'rice': 1450, 'flour': 1200, 'cream cheese': 250, 'chocolate': 200, 'eggs': 10})
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1

You could use a dictionary comprehension to add the elements from both dictionaries:

{key: storage.get(key, 0) + shoppingBag[key] for key in shoppingBag}

Output

{'eggs': 10, 'chocolate': 200, 'cream cheese': 250, 'flour': 1200, 'rice': 1450}
yatu
  • 86,083
  • 12
  • 84
  • 139
1

You can do that like this:

def merge_two_dicts(x, y):
    z = x.copy()
    for k, v in y.items():
        z[k] = z.get(k, 0) + v
    return z

storage = {'flour': 200, 'eggs': 4, 'rice': 450}
shoppingBag = {'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'flour': 1000, 'rice': 1000}

print(merge_two_dicts(shoppingBag, storage))
# {'eggs': 10, 'chocolate': 200, 'cream cheese': 250, 'flour': 1200, 'rice': 1450}
print(merge_two_dicts(storage, shoppingBag))
# {'flour': 1200, 'eggs': 10, 'rice': 1450, 'chocolate': 200, 'cream cheese': 250}
jdehesa
  • 58,456
  • 7
  • 77
  • 121
0

I would recommend using collections.Counter for this. Don't reinvent the wheel.

storage = collections.Counter({'flour': 200, 'eggs': 4, 'rice': 450})
shoppingBag = collections.Counter({'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'flour': 1000, 'rice': 1000})
result = storage + shoppingBag
0x5453
  • 12,753
  • 1
  • 32
  • 61
0
>>> new_dict1 = {key: storage.get(key, 0) + shoppingBag.get(key, 0) for key in set(shoppingBag) | set(storage)}
>>> new_dict1
{'eggs': 10, 'flour': 1200, 'rice': 1450, 'chocolate': 200, 'cream cheese': 250}
0

The Below snippet would help you !!!

Approach 1 :

d1 = {'a': 100, 'b': 200, 'c': 300}
d2 = {'a': 300, 'b': 200, 'd': 400}

for key, value in d1.items():
    if key in d2:
        d1[key] = value + d2[key]
        del d2[key]
    z={**d1,**d2}

print(z)

Approach 2:

import copy

storage = {'flour': 200, 'eggs': 4, 'rice': 450}

shoppingBag = {'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'flour': 1000, 'rice': 1000}

def deep_copy(input_variable):
    return copy.deepcopy(input_variable)

def add_int_recursively(input_variable_1, input_variable_2):
    if input_variable_1 is None:
        return deep_copy(input_variable_2)
    elif input_variable_2 is None:
        return deep_copy(input_variable_1)
    elif isinstance(input_variable_1, int):
        return input_variable_1 + input_variable_2
    else:
        output_object = {}
        for key in set(input_variable_1) | set(input_variable_2):
            output_object[key] = add_int_recursively(input_variable_1.get(key), input_variable_2.get(key))
        return output_object

add_int_recursively(storage,shoppingBag)
redhatvicky
  • 1,912
  • 9
  • 8