I want to merge two dicts on the id. Dict x contains many distinct ids and different row counts per each id. Dict y contains multiple key values, and always has less rows than dict x.
x = [{'costgroup': '1', 'POC1': '2', 'post': '5','id': '1'},
{'costgroup': '2', 'POC1': '1', 'post': '4','id': '1'},
{'costgroup': '3', 'POC1': '5', 'post': '2', 'id': '1'},
{'costgroup': '1', 'POC1': '2', 'post': '5','id': '2'},
{'costgroup': '2', 'POC1': '1', 'post': '4','id': '2'},
{'costgroup': '3', 'POC1': '5', 'post': '2', 'id': '2'},
{'costgroup': '3', 'POC1': '5', 'post': '2', 'id': '2'}]
y = [{'id': '1', 'laminate': 'D'},
{ 'id':'2', 'laminate': T'}]
The output that I want is the following:
z = [{'costgroup': '1', 'POC1': '2', 'post': '5','id': '1','laminate':'D'},
{'costgroup': '2', 'POC1': '1', 'post': '4','id': '1','laminate': 'D'},
{'costgroup': '3', 'POC1': '5', 'post': '2', 'id': '1','laminate': 'D'},
{'costgroup': '1', 'POC1': '2', 'post': '5','id': '2','laminate': 'T'},
{'costgroup': '2', 'POC1': '1', 'post': '4','id': '2','laminate': 'T'},
{'costgroup': '3', 'POC1': '5', 'post': '2', 'id': '2','laminate': 'T'},
{'costgroup': '3', 'POC1': '5', 'post': '2', 'id': '2','laminate': 'T'}]
This is easy to achieve using pandas
dfx = pd.DataFrame(x)
dfy = pd.DataFrame(y)
pd.merge(dfx,dfy, how ='left', left_on = 'id', right_on = 'id' )
But, I am going to apply this using an AWS Lambda function and I don't want to have the overhead of pandas and the output needs to be a dict. I tried the code below which gets me closer, but then i had to add something to find the distinct values of the ID and iterate through them.But,still don't have the output that I need.
valuelist = ['1']
def copyf(dictlist, key, valuelist):
return [d for d in dictlist if d[key] in valuelist]
y1 = copyf(y, 'id', valuelist)
x1 = copyf(x, 'id', valuelist)
y1.append(x1)
The above provides this output, which is interesting but not what I need.
[{'distance': '2', 'id': '1', 'laminate': 'D'},
[{'POC1': '2', 'costgroup': '1', 'id': '1', 'post': '5'},
{'POC1': '1', 'costgroup': '2', 'id': '1', 'post': '4'},
{'POC1': '5', 'costgroup': '3', 'id': '1', 'post': '2'}]]