Merge nested dictionaries, by nested keys?

后端 未结 2 1668
终归单人心
终归单人心 2021-02-09 16:38

I have several dictionaries with different and common keys, plus different and common keys in the nested dictionary. Below is a simplified example, the actual dictionaries have

2条回答
  •  礼貌的吻别
    2021-02-09 17:30

    From your example, looks like you can do something like:

    from collections import defaultdict
    mydict = defaultdict(dict)
    for indict in listofdicts:
        k, v = indict.popitem()
        mydict[k].update(v)
    

提交回复
热议问题