-1

This was one of the question in my technical test round. what could be best way of solving this:

Merging dictionaries - The resultant dict must contain all items of both dicts. If key is common then the value of key in resultant dict must be the sum of value in a and b.

a = {'x': 1, 'y': 2, 'z': 3}

b = {'a': 4, 'b': 5, 'b': 6}


def dictMerge(a, b):

    #Your code here

1 Answers1

0

dict b is not valid as some have pointer out. You can use a look to add the values:

a = {'x': 1, 'y': 2, 'z': 3, 'b': 6}

b = {'a': 4, 'b': 5}


def dict_merge(a, b):
    result = a.copy()
    for key, value in b.items():
        result[key] = result.get(key, 0) + value
    return result

print(dict_merge(a, b))
miquelvir
  • 1,748
  • 1
  • 7
  • 21