In your case, you can do:
z = dict(list(x.items()) + list(y.items()))
This will, as you want it, put the final dict in z
, and make the value for key b
be properly overridden by the second (y
) dict's value:
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 10, 'c': 11}
>>> z = dict(list(x.items()) + list(y.items()))
>>> z
{'a': 1, 'c': 11, 'b': 10}
If you use Python 2, you can even remove the list()
calls. To create z:
>>> z = dict(x.items() + y.items())
>>> z
{'a': 1, 'c': 11, 'b': 10}
If you use Python version 3.9.0a4 or greater, then you can directly use:
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 10, 'c': 11}
>>> z = x | y
print(>>> z)
{'a': 1, 'c': 11, 'b': 10}