0

I have read through lots about nested merging, but could find a way to merge dictionaries properly, even in an infinite dictionary nest. Can someone help me out please.

from collections import defaultdict
dictA ={"core":{"globals":{"qmenu_object_name":"menu2",
                            "metadata_label":"label1"},
        "load_plug":True}}

dictB = {"core":{"globals":{"widget_name":"widgetA"}}}
mydicts = [dictA, dictB]
result = defaultdict(dict)
for d in mydicts:
    for k, v in d.iteritems():
        result[k].update(v)

I expect this:

dictA ={"core":{"globals":{"qmenu_object_name":"menu2",
                            "metadata_label":"label1",
                            "widget_name":"widgetA"},
        "load_plug":True}}

But I get this:

"core":{"globals":{"widget_name":"widgetA"}, 'load_plug': True}

Thanks

arvidurs
  • 2,853
  • 4
  • 25
  • 36
  • You need a general solution, not just one for your example `dictA` and `dictB`, correct? – timgeb Jan 27 '16 at 16:31
  • dict.update() do add new keys or overwrite existing keys. I assume, if you need infinite deep, you should write some recursion func – The Godfather Jan 27 '16 at 16:34

0 Answers0