merging a list of nested dictionaries

Question:

I have two list of dictionaries as shown in example below

list1=[
        {
            "pdpData":{
                "a":1,
                "b":2
            }
        }
    ]

list2=[
    {
        "pdpData":{
            "a":1,
            "c":3
        }
    },
    {
        "pdpData":{
            "a":2,
            "b":3
        }
    }
]

I want the result as shown in the format below

list3=[
{
    "pdpData":{
        "a":1,
        "b":2,
        "c":3
    }
},
{
    "pdpData":{
        "a":2,
        "b":3
    }
}
]

The size of list1 and list2 could be in 10000’s. List3 would be the union of list1 and list2. What could be the best pythonic solutions to solve this problem.

Asked By: nishant kumar

||

Answers:

You didn’t write any code, so I won’t write a complete solution. You’ll need zip_longest and dict merging.

from itertools import zip_longest

list1=[
        {
            "pdpData":{
                "a":1,
                "b":2
            }
        }
    ]

list2=[
    {
        "pdpData":{
            "a":1,
            "c":3
        }
    },
    {
        "pdpData":{
            "a":2,
            "b":3
        }
    }
]


for d1, d2 in zip_longest(list1, list2):
    dd1 = d1.get("pdpData", {}) if d1 else {}
    dd2 = d2.get("pdpData", {}) if d2 else {}
    print({**dd1, **dd2})

It outputs :

{'a': 1, 'b': 2, 'c': 3}
{'a': 2, 'b': 3}

Now that you have merged inner-dicts, all you need to do is pack them into another dict with "pdpData" as key, and pack those dicts into a list.

Answered By: Eric Duminil
from collections import defaultdict

d = defaultdict(dict)
for l in (l1, l2):
    for elem in l:
        d[elem['pdpData']['a']].update(elem['pdpData'])
l3 = d.values()

print(l3)

Output

dict_values([{'a': 1, 'b': 2, 'c': 3}, {'a': 2, 'b': 3}])
Answered By: Van Peer
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.