Search code examples
pythonlistdictionarymerge

How to identify duplicate dictionaries and merge its sub list into one dictionary


I have a list with duplicate dictionaries based on the "id" key. I want to merge the sublist in the dictionary and create 1 dictionary out of the 2.

Ive tried a few things but my python skills are a bit rusty

[
  {
   "id": "1"
   "sublist": [
     {
       "key":"1"
     }
   ]
  },
  {
   "id": "1"
   "sublist": [
     {
       "key":"2"
     }
   ]
  },
  {
   "id": "2"
   "sublist": [
     {
       "key":"3"
     }
   ]
  }
]

Final result should be this:

[
  {
   "id": "1"
   "sublist": [
     {
       "key":"1",
     },
     {
       "key":"2"
     }
   ]
  },
  {
   "id": "2"
   "sublist": [
     {
       "key":"3"
     }
   ]
  }
]

Solution

  • One way to do this is, you create a dictionary merged_data where the keys are the "id" values from your original list. You then iterate over each dictionary in the original list and either add the "sublist" items to an existing entry in merged_data (if the "id" is already there) or create a new entry (if the "id" is not present). Finally, you convert the merged dictionary back to a list.

    Here is an example implementation:

    merged_data = {}
    
    # iterate through your original list (called data here) and merge dictionaries with the same id
    for item in data:
        item_id = item["id"]
        if item_id in merged_data:
            merged_data[item_id]["sublist"].extend(item["sublist"])
        else:
            merged_data[item_id] = item
    
    # convert the merged dictionary back to a list
    result = list(merged_data.values())
    
    print(result)