How to merge multiple dicts with same key?

前端 未结 14 2529
别跟我提以往
别跟我提以往 2020-11-22 13:12

I have multiple dicts/key-value pairs like this:

d1 = {key1: x1, key2: y1}  
d2 = {key1: x2, key2: y2}  

I want the result to be a new di

14条回答
  •  自闭症患者
    2020-11-22 13:35

    A compact possibility

    d1={'a':1,'b':2}
    d2={'c':3,'d':4}
    context={**d1, **d2}
    context
    {'b': 2, 'c': 3, 'd': 4, 'a': 1}
    

提交回复
热议问题