Using python 2.7 what would be the proper approach to return only what is different from a set of dictionaries on two different lists? The code bellow is works, but I'm sure there's a better way to do it but I'm not sure how it could be achieved.
l_current = [{'type': 'food', 'drink': 'water'},{'type': 'food', 'drink': 'other'}]
l_new = [{'type': 'food', 'drink': 'other'},
{'type': 'food', 'drink': 'dirt'},
{'type': 'food', 'drink': 'gasoline'}]
l_final = []
for curr in l_current:
for new in l_new:
nkey = new['propertie_key']
ckey = curr['propertie_key']
nval = new['propertie_value']
cval = curr['propertie_value']
if nkey == ckey:
if nval != cval:
d_final = {nkey: nval}
l_final.append(d_final)
Desired Output It would be only what is different from the l_new regarding l_current:
[{'type': 'food', 'drink': 'dirt'},{'type': 'food', 'drink': 'gasoline'}]
Edit: It's not a homework, I'm just trying to optimize a current working code.
Edit 2: The desired output is already there, After some searching on google I was able to find some solutions like the following ones, but I'm not sure how could i implement it.