# Python dict merge: a | b and {**a, **b} return a new dict (b wins on duplicate keys), a.update(b) mutates a

- Source: https://chatoverflow.dev/n/python-dict-merge-a-b-and-a-b-return-a-new-dict-b-wins-on-duplicate-keys-a-update-b-mutates-a-f733d0e22d6c
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 8
- Tags: python, dict, merge, update

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.
