dict1 = [{'id': 1.0, 'name': 'aa'},
{'id': 4.0, 'name': 'bb'},
{'id': 2.0, 'name': 'cc'}]
and
dict2 = [{'name': 'aa', 'dtype': 'StringType'},
{'name': 'bb', 'dtype': 'StringType'},
{'name': 'xx', 'dtype': 'StringType'},
{'name': 'cc', 'dtype': 'StringType'}]
I would like to merge this two dictionaries based on their common key which is name.
I would like to get the following desired result.
merged_dict= [{'id': 1.0, 'name': 'aa', 'dtype': 'StringType'},
{'id': 4.0, 'name': 'bb', 'dtype': 'StringType'},
{'id': 2.0, 'name': 'cc', 'dtype': 'StringType'}]
I was trying to get this using the following for loop.
for i in dict1:
for j in dict2:
j.update(i)
>Solution :
To avoid quadratic complexity, better first create a real dictionary (yours are lists of dictionaries), then update:
tmp = {d['name']: d for d in dict2}
for d in dict1:
d.update(tmp.get(d['name'], {}))
print(dict1)
Output:
[{'id': 1.0, 'name': 'aa', 'dtype': 'StringType'},
{'id': 4.0, 'name': 'bb', 'dtype': 'StringType'},
{'id': 2.0, 'name': 'cc', 'dtype': 'StringType'}]
Intermediate tmp:
{'aa': {'name': 'aa', 'dtype': 'StringType'},
'bb': {'name': 'bb', 'dtype': 'StringType'},
'xx': {'name': 'xx', 'dtype': 'StringType'},
'cc': {'name': 'cc', 'dtype': 'StringType'}}
If you want a copy (rather that modifying dict1 in place):
tmp = {d['name']: d for d in dict2}
merged_dict = [d|tmp.get(d['name'], {}) for d in dict1]