Reputation: 539
Good day , I have this problem in python, where i want to merge dictionary list of dictionary.. here is the thing i want to merge:
a = {
"0": {
"Holder": "23002154-1",
"HolderJob": "243340545",
"IsControl": "N",
"IsSPC": "N",
"LoadPosition": "5",
"MeasurementType": "XRF",
"PalletName": "12",
"PalletPosition": "1",
"ProcessToolName": "DLCX01",
"RecipeName": "APC_14A_COC_Al2O3_Fill-TEST",
"RunNumber": "4613"
},
"1": {
"Holder": "23002158-1",
"HolderJob": "243340544",
"IsControl": "N",
"IsSPC": "N",
"LoadPosition": "9",
"MeasurementType": "XRF",
"PalletName": "12",
"PalletPosition": "1",
"ProcessToolName": "DLCX01",
"RecipeName": "APC_14A_COC_Al2O3_Fill-TEST",
"RunNumber": "4613"
}
}
b = {
"0": {
"Claimable": "\"false\"",
"Experiment": "\"264644\"",
"HTNum": "\"516\"",
"HolderType": "\"CARJOB\"",
"MinorRev": "\"140688\"",
"Operation": "\"510150 DLCX DEPOSITION\"",
"ParentHolder": "\"23002158\"",
"ProductName": "\"AE_T_B\"",
"WaferEC": "\"140517\""
},
"1": {
"Claimable": "\"false\"",
"Experiment": "\"264644\"",
"HTNum": "\"516\"",
"HolderType": "\"CARJOB\"",
"MinorRev": "\"140688\"",
"Operation": "\"510150 DLCX DEPOSITION\"",
"ParentHolder": "\"23002158\"",
"ProductName": "\"AE_T_B\"",
"WaferEC": "\"140517\""
}
}
example output must be:
merge_a_b = {
"0": {
"Holder": "23002154-1",
"HolderJob": "243340545",
"IsControl": "N",
"IsSPC": "N",
"LoadPosition": "5",
"MeasurementType": "XRF",
"PalletName": "12",
"PalletPosition": "1",
"ProcessToolName": "DLCX01",
"RecipeName": "APC_14A_COC_Al2O3_Fill-TEST",
"RunNumber": "4613",
"Claimable": "\"false\"",
"Experiment": "\"264644\"",
"HTNum": "\"516\"",
"HolderType": "\"CARJOB\"",
"MinorRev": "\"140688\"",
"Operation": "\"510150 DLCX DEPOSITION\"",
"ParentHolder": "\"23002158\"",
"ProductName": "\"AE_T_B\"",
"WaferEC": "\"140517\""
},
"1": {
"Holder": "23002158-1",
"HolderJob": "243340544",
"IsControl": "N",
"IsSPC": "N",
"LoadPosition": "9",
"MeasurementType": "XRF",
"PalletName": "12",
"PalletPosition": "1",
"ProcessToolName": "DLCX01",
"RecipeName": "APC_14A_COC_Al2O3_Fill-TEST",
"RunNumber": "4613",
"Claimable": "\"false\"",
"Experiment": "\"264644\"",
"HTNum": "\"516\"",
"HolderType": "\"CARJOB\"",
"MinorRev": "\"140688\"",
"Operation": "\"510150 DLCX DEPOSITION\"",
"ParentHolder": "\"23002158\"",
"ProductName": "\"AE_T_B\"",
"WaferEC": "\"140517\""
}
}
so far i've used this code but it seems not to work:
def merge_dict(dict1,dict2):
dictio = dict(dict1,**dict2)
return dictio
Upvotes: 2
Views: 85
Reputation: 90879
Yes, dict(dict1,**dict2)
, would not work because it would simply overwrite the value for the key with the new value that comes later (from dict2
). What you should do is -
def merge_dict(dict1,dict2):
resdict = {}
for k,v in dict2.items():
resdict[k] = dict(v)
resdict[k].update(dict1.get(k,{})))
return resdict
Demo -
>>> a = {
... "0": {
... "Holder": "23002154-1",
... "HolderJob": "243340545",
... "IsControl": "N",
... "IsSPC": "N",
... "LoadPosition": "5",
... "MeasurementType": "XRF",
... "PalletName": "12",
... "PalletPosition": "1",
... "ProcessToolName": "DLCX01",
... "RecipeName": "APC_14A_COC_Al2O3_Fill-TEST",
... "RunNumber": "4613"
... },
... "1": {
... "Holder": "23002158-1",
... "HolderJob": "243340544",
... "IsControl": "N",
... "IsSPC": "N",
... "LoadPosition": "9",
... "MeasurementType": "XRF",
... "PalletName": "12",
... "PalletPosition": "1",
... "ProcessToolName": "DLCX01",
... "RecipeName": "APC_14A_COC_Al2O3_Fill-TEST",
... "RunNumber": "4613"
... }
... }
>>>
>>> b = {
... "0": {
... "Claimable": "\"false\"",
... "Experiment": "\"264644\"",
... "HTNum": "\"516\"",
... "HolderType": "\"CARJOB\"",
... "MinorRev": "\"140688\"",
... "Operation": "\"510150 DLCX DEPOSITION\"",
... "ParentHolder": "\"23002158\"",
... "ProductName": "\"AE_T_B\"",
... "WaferEC": "\"140517\""
... },
... "1": {
... "Claimable": "\"false\"",
... "Experiment": "\"264644\"",
... "HTNum": "\"516\"",
... "HolderType": "\"CARJOB\"",
... "MinorRev": "\"140688\"",
... "Operation": "\"510150 DLCX DEPOSITION\"",
... "ParentHolder": "\"23002158\"",
... "ProductName": "\"AE_T_B\"",
... "WaferEC": "\"140517\""
... }
... }
>>> def merge_dict(dict1,dict2):
... resdict = {}
... for k,v in dict2.items():
... resdict[k] = dict(v)
... resdict[k].update(dict1.get(k,{})))
... return resdict
...
>>> merged_a_b = merge_dict(a,b)
>>> import pprint
>>> pprint.pprint(merged_a_b)
{'0': {'Claimable': '"false"',
'Experiment': '"264644"',
'HTNum': '"516"',
'Holder': '23002154-1',
'HolderJob': '243340545',
'HolderType': '"CARJOB"',
'IsControl': 'N',
'IsSPC': 'N',
'LoadPosition': '5',
'MeasurementType': 'XRF',
'MinorRev': '"140688"',
'Operation': '"510150 DLCX DEPOSITION"',
'PalletName': '12',
'PalletPosition': '1',
'ParentHolder': '"23002158"',
'ProcessToolName': 'DLCX01',
'ProductName': '"AE_T_B"',
'RecipeName': 'APC_14A_COC_Al2O3_Fill-TEST',
'RunNumber': '4613',
'WaferEC': '"140517"'},
'1': {'Claimable': '"false"',
'Experiment': '"264644"',
'HTNum': '"516"',
'Holder': '23002158-1',
'HolderJob': '243340544',
'HolderType': '"CARJOB"',
'IsControl': 'N',
'IsSPC': 'N',
'LoadPosition': '9',
'MeasurementType': 'XRF',
'MinorRev': '"140688"',
'Operation': '"510150 DLCX DEPOSITION"',
'PalletName': '12',
'PalletPosition': '1',
'ParentHolder': '"23002158"',
'ProcessToolName': 'DLCX01',
'ProductName': '"AE_T_B"',
'RecipeName': 'APC_14A_COC_Al2O3_Fill-TEST',
'RunNumber': '4613',
'WaferEC': '"140517"'}}
Upvotes: 2