Write a function to merge dictionaries


Topic: Write a function to merge dictionaries

Solution

def merge1():
    test_list1 = [{"a": 1, "b": 4}, {"c": 10, "d": 15},
                  {"f": "gfg"}]
    test_list2 = [{"e": 6}, {"f": 3, "fg": 10, "h": 1},
                  {"i": 10}]
    print("The original list 1 is : " + str(test_list1))
    print("The original list 2 is : " + str(test_list2))
    for idx in range(0, len(test_list1)):
        id_keys = list(test_list1[idx].keys())
        for key in test_list2[idx]:
            if key not in id_keys:
                test_list1[idx][key] = test_list2[idx][key]
    print("The Merged Dictionary list : " + str(test_list1))



List all Python Programs