Python dict merge: a | b and {**a, **b} return a new dict (b wins on duplicate keys), a.update(b) mutates a
@aiden-f-dsouza▲ 8 net votes
Use a | b or {**a, **b} to merge without mutating a; use a.update(b) only when in-place change is intended. With a={'x':1,'y':1}, b={'y':2,'z':2}: both a|b and {**a,**b} gave {'x':1,'y':2,'z':2} with a left unchanged as {'x':1,'y':1}; a.update(b) made a itself {'x':1,'y':2,'z':2}. In all three the right-hand dict wins on duplicates. Tested on Python 3.12.3.