Given two List of dictionaries with possible duplicate keys, write a Python program to perform merge.
Input : test_list1 = [{"gfg" : 1, "best" : 4}, {"geeks" : 10, "good" : 15}, {"love" : "gfg"}], test_list2 = [{"gfg" : 6}, {"better" : 3, "for" : 10, "geeks" : 1}, {"gfg" : 10}]
Output : [{'gfg': 1, 'best': 4}, {'geeks': 10, 'good': 15, 'better': 3, 'for': 10}, {'love': 'gfg', 'gfg': 10}]
Explanation : gfg while merging retains value of 1, and "best" is added to dictionary as key from other list's 1st dictionary ( same index ).
Input : test_list1 = [{"gfg" : 1, "best" : 4}, {"love" : "gfg"}], test_list2 = [{"gfg" : 6}, {"gfg" : 10}]
Output : [{'gfg': 1, 'best': 4}, {'love': 'gfg', 'gfg': 10}]
Explanation : gfg while merging retains value of 1, and "best" is added to dictionary as key from other list's 1st dictionary ( same index ).
In this we reconstruct the key value pair in accordance of all the keys not recurring, checking using in operator and extracting keys using keys().
The original list 1 is : [{'gfg': 1, 'best': 4}, {'geeks': 10, 'good': 15}, {'love': 'gfg'}] The original list 2 is : [{'gfg': 6}, {'better': 3, 'for': 10, 'geeks': 1}, {'gfg': 10}] The Merged Dictionary list : [{'gfg': 1, 'best': 4}, {'geeks': 10, 'good': 15, 'better': 3, 'for': 10}, {'love': 'gfg', 'gfg': 10}]
OutputThe Merged Dictionary list : [{'best': 4, 'gfg': 1}, {'geeks': 10, 'for': 10, 'better': 3, 'good': 15}, {'love': 'gfg', 'gfg': 10}]