How to merge two dicts and combine common keys?

后端 未结 3 1179
执念已碎
执念已碎 2021-01-17 01:35

I would like to know how if there exists any python function to merge two dictionary and combine all values that have a common key.

I have found function to append t

3条回答
  •  走了就别回头了
    2021-01-17 02:18

    There is no built-in function for this but you can use a defaultdict for this:

    from collections import defaultdict
    d = defaultdict(list)
    for other in [d1, d1]:
        for k, v in other.items():
            d[k].append(v)
    

提交回复
热议问题