Open In App

Python – Merge Dictionaries List with duplicate Keys

Last Updated : 13 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given two List of dictionaries with possible duplicate keys, write a Python program to perform merge.

Examples:

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 ). 

Approach : Using loop + keys()

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().

Python3




# Python3 code to demonstrate working of
# Merge Dictionaries List with duplicate Keys
# Using loop + keys()
 
# initializing lists
test_list1 = [{"gfg": 1, "best": 4}, {"geeks": 10, "good": 15},
              {"love": "gfg"}]
 
test_list2 = [{"gfg": 6}, {"better": 3, "for": 10, "geeks": 1},
              {"gfg": 10}]
 
# printing original lists
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)):
 
    # getting keys of corresponding index
    id_keys = list(test_list1[idx].keys())
    for key in test_list2[idx]:
 
        # checking for keys presence
        if key not in id_keys:
            test_list1[idx][key] = test_list2[idx][key]
 
# printing result
print("The Merged Dictionary list : " + str(test_list1))


Output:

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}]

Time Complexity: O(n*n)
Auxiliary Space: O(n)

Approach: Using dict comprehension and set union

  • First, create a list of all the unique keys present in both dictionaries across both lists using set union.
  • Then, for each dictionary in both lists, create a new dictionary using dict comprehension where the keys are taken from the set created in step 1 and the values are obtained by checking if the key is present in the dictionary and getting the corresponding value. If the key is not present in the dictionary, get the value from the other list’s dictionary for the same key.
  • Finally, combine the new dictionaries created for each dictionary in both lists into a single list using list comprehension.

Python3




# initializing lists
test_list1 = [{"gfg": 1, "best": 4}, {"geeks": 10, "good": 15}, {"love": "gfg"}]
test_list2 = [{"gfg": 6}, {"better": 3, "for": 10, "geeks": 1}, {"gfg": 10}]
 
# create a set of all unique keys in both dictionaries
all_keys = set().union(*(d.keys() for d in test_list1 + test_list2))
 
# create new dictionaries with merged values for each dictionary in both lists
new_list1 = [{key: test_list1[idx][key] if key in test_list1[idx] else None for key in all_keys} for idx in range(len(test_list1))]
new_list2 = [{key: test_list2[idx][key] if key in test_list2[idx] else None for key in all_keys} for idx in range(len(test_list2))]
 
# combine the new dictionaries into a single list
merged_list = []
for dict1, dict2 in zip(new_list1, new_list2):
    merged_dict = {}
    for key in all_keys:
        if dict1[key] is not None:
            merged_dict[key] = dict1[key]
        elif dict2[key] is not None:
            merged_dict[key] = dict2[key]
    merged_list.append(merged_dict)
 
# printing result
print("The Merged Dictionary list : " + str(merged_list))


Output

The Merged Dictionary list : [{'best': 4, 'gfg': 1}, {'geeks': 10, 'for': 10, 'better': 3, 'good': 15}, {'love': 'gfg', 'gfg': 10}]

Time complexity: O(nk), where n is the length of the input lists and k is the average number of keys in each dictionary.
Auxiliary space: O(nk), as we create new dictionaries for each input dictionary.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads