Compare dicts and merge them. No overwrite and no duplicate values

Here is how I would go about it:

d1 = {'a': ['a'], 'b': ['b', 'c']}
d2 = {'b': ['c', 'd'], 'c': ['e','f']}
dd1 = {**d1, **d2}
dd2 = {**d2, **d1}
{k:list(set(dd1[k]).union(set(dd2[k]))) for k in dd1}

Produces the desired result.


I suggest using a default dictionary collection with a set as a default value. It guarantees that all values will be unique and makes the code cleaner.

Talking about efficiecy it's O(n^2) by time.

   from collections import defaultdict


   d1 = {'a': ['a'], 'b': ['b', 'c']}
   d2 = {'b': ['c', 'd'], 'c': ['e','f']}

   new_dict = defaultdict(set)

   for k, v in d1.items():
       new_dict[k] = new_dict[k].union(set(v))

   for k, v in d2.items():
       new_dict[k] = new_dict[k].union(set(v))


Try this code. You can remove deep copy if modifications in the initial array are fine for you.

import copy


def merge(left, right):
    res = copy.deepcopy(left)

    for k, v in right.items():
        res[k] = list(set(res[k]).union(v)) if k in res else v

    return res