I have two list of dictionaries as follows:
x=[{'id':1, 'var1':2},
{'id':1, 'var1':2},
{'id':2, 'var1':2}]
y=[{'var2':1, 'var3':2},
{'var2':3, 'var3':3},
{'var2':4, 'var3':4}]
I want to merge each dict into each other dict, so the output is as follows:
z=[{'id':1, 'var1':2, 'var2':1, 'var3':2},
{'id':1, 'var1':2, 'var2':3, 'var3':3},
{'id':2, 'var1':2, 'var2':4, 'var3':4}]
However, when I use the following merge function:
def merge_x_and_y(x_data, y_data):
for x_entry, y_entry in zip(x_data, y_data):
x_entry.update(y_entry )
return x_data
I instead end up with:
z=[{'id':1, 'var1':2, 'var2':1, 'var3':2},
{'id':1, 'var1':2, 'var2':1, 'var3':2},
{'id':2, 'var1':2, 'var2':2, 'var3':3}]
So if a dict in x is a duplicate, it duplicates the dict from y, which I don't want. I want each x dict to be merged with a different y dict (even if the x dict is a duplicate).
Is there a way to merge each x dict with a y dict ignoring duplicates?
Edit:
The reprex above works, but I still don't get the right outcome in my actual code. I think it might be to do with Lenik's suggestion, that the same elements may be referenced twice.
Prior to the merge, I've tried to expand the data using:
def expand_x(x):
processed_x_data = []
[processed_x_data .extend([entry]*entry['count']) for entry in x]
return processed_x_data
After doing this step, should I reallocate a unique id to each entry to avoid double referencing?