How do I merge dictionaries together in Python?

后端 未结 7 1793
无人及你
无人及你 2020-12-04 17:05
d3 = dict(d1, **d2)

I understand that this merges the dictionary. But, is it unique? What if d1 has the same key as d2 but different value? I woul

7条回答
  •  庸人自扰
    2020-12-04 17:43

    If you want d1 to have priority in the conflicts, do:

    d3 = d2.copy()
    d3.update(d1)
    

    Otherwise, reverse d2 and d1.

提交回复
热议问题