4

I'm looking to merge two dictionaries shown below, but I was unsuccessful in doing so.

I have read many blog posts, but I did not find an answer.

dict1={"KPNS": {"metadocdep": {"eta": {"sal": "2"}}, "metadocdisp": {"meta": {"head": "1"}}}, "EGLS": {"apns": {"eta": {"sal": "2"}}, "gcm": {"meta": {"head": "1"}}}}

dict2={"KPNS": {"metadocdep": {"eta": {"sal": "7"}}, "metadocdisp": {"meta": {"head": "5"}}}, "EGLS": {"apns": {"eta": {"sal": "7"}}, "gcm": {"meta": {"head": "9"}}}}
finaldict = {key:(dict1[key], dict2[key]) for key in dict1}

print finaldict

My final output should be like:

{"KPNS": {"metadocdep": {"eta": {"sal": [2,7]}},
          "metadocdisp": {"meta": {"head": [1,5]}}},
 "EGLS": {"apns": {"eta": {"sal": [2,7]}},
          "gcm": {"meta": {"head": [1,9]}}}}

How can I do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
saketh modugu
  • 53
  • 1
  • 5

1 Answers1

3

With defaultdict

If you know what the expected depth is, you could use nested defaultdicts to define final_dict:

from collections import defaultdict

final_dict = defaultdict(lambda: defaultdict(lambda: defaultdict(list)))

final_dict['a']['b']['c'].append(1)
print(final_dict)
# defaultdict(<function <lambda> at 0x7f2ae7f41e18>, {'a': defaultdict(<function <lambda>.<locals>.<lambda> at 0x7f2ae636b730>, {'b': defaultdict(<class 'list'>, {'c': [1]})})})

There's a lot of added output because of the defaultdict, but you could treat final_dict as a simple dict.

With dicts

With standard dicts, you'll have to use setdefault. The code doesn't become very readable, though:

dict1 = {"KPNS": {"metadocdep": {"eta": {"sal": "2"}}, "metadocdisp": {"meta": {
    "head": "1"}}}, "EGLS": {"apns": {"eta": {"sal": "2"}}, "gcm": {"meta": {"head": "1"}}}}

dict2 = {"KPNS": {"metadocdep": {"eta": {"sal": "7"}}, "metadocdisp": {"meta": {
    "head": "5"}}}, "EGLS": {"apns": {"eta": {"sal": "7"}}, "gcm": {"meta": {"head": "9"}}}}

final_dict = {}
for d in [dict1, dict2]:
    for level1 in d:
        for level2 in d[level1]:
            for level3 in d[level1][level2]:
                for level4 in d[level1][level2][level3]:
                    final_dict.setdefault(level1, {}).setdefault(level2, {}).setdefault(
                        level3, {}).setdefault(level4, []).append(d[level1][level2][level3][level4])

print(final_dict)
# {'KPNS': {'metadocdep': {'eta': {'sal': ['2', '7']}}, 'metadocdisp': {'meta': {'head': ['1', '5']}}}, 'EGLS': {'apns': {'eta': {'sal': ['2', '7']}}, 'gcm': {'meta': {'head': ['1', '9']}}}}

It might be a bit more efficient with dict.items():

for d in [dict1, dict2]:
    for level1, d2s in d.items():
        for level2, d3s in d2s.items():
            for level3, d4s in d3s.items():
                for level4, v in d4s.items():
                    final_dict.setdefault(level1, {}).setdefault(level2, {}).setdefault(
                        level3, {}).setdefault(level4, []).append(v)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124