I have two lists of dictionaries and I'd like to merge them. When a dictionary is present in both lists, I'd like to add a "confidence" key to the dictionary to reflect that the dictionary was present in both lists.
List-1
lst1 = [
{'key': 'data_collected.service_data'},
{'key': 'gdpr.gdpr_compliance'},
{'key': 'disclosure_of_information.purpose_of_disclosure'},
{'key': 'opt_out.choice_of_opt_out'}
]
List-2
lst2 = [
{'key': 'child_data_protection.parent_guardian_consent'},
{'key': 'ccpa.ccpa_compliance'},
{'key': 'disclosure_of_information.purpose_of_disclosure'},
{'key': 'opt_out.choice_of_opt_out'}
]
when i run below code i am not getting proper output
res = []
for x in lst1:
for y in lst2:
if x["key"] == y["key"]:
if x not in res and y not in res:
res.append({"key": x["key"], "confidence": 1})
else:
if x not in res and y not in res:
res.append(x)
res.append(y)
print(res)
OUTPUT should like
[
{'key': 'data_collected.service_data'},
{'key': 'gdpr.gdpr_compliance'},
{
'key': 'disclosure_of_information.purpose_of_disclosure',
'confidence': 1
},
{
'key': 'opt_out.choice_of_opt_out',
'confidence': 1
},
{'key': 'child_data_protection.parent_guardian_consent'},
{'key': 'ccpa.ccpa_compliance'}
]
Another approach can be:
lst1 = [
{'key': 'data_collected.service_data'},
{'key': 'gdpr.gdpr_compliance'},
{'key': 'disclosure_of_information.purpose_of_disclosure'},
{'key': 'opt_out.choice_of_opt_out'}
]
lst2 = [
{'key': 'child_data_protection.parent_guardian_consent'},
{'key': 'ccpa.ccpa_compliance'},
{'key': 'disclosure_of_information.purpose_of_disclosure'},
{'key': 'opt_out.choice_of_opt_out'}
]
for data in lst1:
# If same data exists in lst2, add confidence key and remove it from lst2
if data in lst2:
lst2.remove(data)
data['confidence']=1
# At the end of above for loop, lst2 contains unique data, now just add both the lists to get the final result
lst1 = lst1+lst2
print (lst1)
Output:
[{'key': 'data_collected.service_data'}, {'key': 'gdpr.gdpr_compliance'}, {'key': 'disclosure_of_information.purpose_of_disclosure', 'confidence': 1}, {'key': 'opt_out.choice_of_opt_out', 'confidence': 1}, {'key': 'child_data_protection.parent_guardian_consent'}, {'key': 'ccpa.ccpa_compliance'}]