[Solved] How to merge two dictionaries with same keys and keep values that exist in the same key [closed]


def intersect(a, b):
    a, b = set(a), set(b)
    return list(a & b)


def merge_dicts(d1, d2):
    return {k: intersect(v1, v2) for (k, v1), v2 in zip(d1.items(), d2.values())}


dictionary_1 = {'A' :['B','C','D'],'B' :['A']}
dictionary_2 = {'A' :['A','B'],    'B' :['A','F']}

merge = merge_dicts(dictionary_1, dictionary_2)
print(merge) # {'A': ['B'], 'B': ['A']}

4

solved How to merge two dictionaries with same keys and keep values that exist in the same key [closed]