2

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 
iamcoder
  • 529
  • 2
  • 4
  • 23
  • Possible duplicate of [How can I merge two Python dictionaries in a single expression?](http://stackoverflow.com/questions/38987/how-can-i-merge-two-python-dictionaries-in-a-single-expression) – uranusjr Oct 06 '15 at 01:32
  • Hi sir, i already saw that question but it seems not the applicable algorithm for the output what i want to expect. Thanks for the help thou..:) – iamcoder Oct 06 '15 at 03:44

1 Answers1

2

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"'}}
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • wow problem solved!. This is the best possible solution to implement. when trying to merge two dictionary of dictionary. Thanks for the fast response sir Kumar. – iamcoder Oct 06 '15 at 01:37
  • I would not use the ** trick. It's fun to know but it will break if any of the keys is not a valid identifier. Just stick to `update`. – spectras Oct 06 '15 at 01:37
  • @spectras hmm, I am guessing you are talking about keys that are not string? – Anand S Kumar Oct 06 '15 at 01:40
  • This, or any string that is not valid as an identifier, for instance "-". It happens to work on CPython but it is an implementation detail and there is no guarantee it works on other implementations, or even on future CPython versions. – spectras Oct 06 '15 at 01:42