Search code examples
pythondictionarymergeunique

Merging dictionaries into one list of dictionaries on a unique id


I'm not sure how to best describe what I'm after - it's not exactly a straight merge of dictionaries so adding them together or unpacking them isn't going to work. It's probably easier to compare what I want to a pandas merge - join on a common key which will result in additional columns/rows depending on the inputs.

I am starting with the below lists:

a = [{'GlobalID': '1e6afb53-9276-495a-81e0-1462b765fa67', 'aResult': 1}]
b = [{'GlobalID': '1e6afb53-9276-495a-81e0-1462b765fa67', 'bResult': 1}]
c = [{'GlobalID': '1e6afb53-9276-495a-81e0-1462b765fa67', 'cResult': 1}]
d = [{'GlobalID': '1e6afb53-9276-495a-81e0-1462b765fa67', 'dResult': 0},
  {'GlobalID': '43e405ee-a680-4958-a3c4-e64344a04786', 'dResult': 1},
  {'GlobalID': '2914fe6f-483c-479e-a1fa-2817737546bf', 'dResult': 0}
]

And I want to merge/combine them such that I end up only with the unique GlobalIDs and any corresponding results:

[
 {'GlobalID': '1e6afb53-9276-495a-81e0-1462b765fa67', 'aResult': 1, 'bResult': 1, 'cResult': 1, 'dResult': 0}, 
 {'GlobalID': '43e405ee-a680-4958-a3c4-e64344a04786', 'dResult': 1}, 
 {'GlobalID': '2914fe6f-483c-479e-a1fa-2817737546bf', 'dResult': 0}
]

Is there a straightforward way of doing this? I'd appreciate any ideas/resources people can point me to.

Thanks!


Solution

  • lists = [a, b, c , d] # consider you have more than 4 lists
    merged_dicts = dict({}) # create a dictionary to save the result
    for l in lists: # loop on the lists
        for doc in l: # loop on the documents on each list
            GlobalID = doc['GlobalID'] # get the id of the document
            if GlobalID in merged_dicts: # check if we already found a doc with the same id
                old_doc = merged_dicts[GlobalID] # if yes we get the old merged doc
                for key in doc: # we loop over the contents of the new document
                    old_doc[key] = doc[key] # we add the values to the doc result
                merged_dicts[GlobalID] = old_doc # and we change the result in the result dictionary
            else: # if not add the doc to the dictionary
                merged_dicts[GlobalID] = doc
    merged_dicts.values()
    

    Output:

    [{'GlobalID': '43e405ee-a680-4958-a3c4-e64344a04786', 'dResult': 1},
     {'GlobalID': '1e6afb53-9276-495a-81e0-1462b765fa67',
      'aResult': 1,
      'bResult': 1,
      'cResult': 1,
      'dResult': 0},
     {'GlobalID': '2914fe6f-483c-479e-a1fa-2817737546bf', 'dResult': 0}]