How to merge two Python dictionaries in a single expression?
<p>I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged.  The <code>update()</code> method would be what I need, if it returned its result instead of modifying a dict in-place.</p>

<pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2}
&gt;&gt;&gt; y = {'b':10, 'c': 11}
&gt;&gt;&gt; z = x.update(y)
&gt;&gt;&gt; print z
None
&gt;&gt;&gt; x
{'a': 1, 'b': 10, 'c': 11}
</code></pre>

<p>How can I get that final merged dict in z, not x?</p>

<p>(To be extra-clear, the last-one-wins conflict-handling of <code>dict.update()</code> is what I'm looking for as well.)</p>

<p>In your case, what you can do is:</p>

<pre><code>z = dict(x.items() + y.items())
</code></pre>

<p>This will, as you want it, put the final dict in <code>z</code>, and make the value for key <code>b</code> be properly overridden by the second (<code>y</code>) dict's value:</p>

<pre><code>&gt;&gt;&gt; x = {'a':1, 'b': 2}
&gt;&gt;&gt; y = {'b':10, 'c': 11}
&gt;&gt;&gt; z = dict(x.items() + y.items())
&gt;&gt;&gt; z
{'a': 1, 'c': 11, 'b': 10}
</code></pre>

<p>If you use Python 3, it is only a little more complicated.  To create <code>z</code>:</p>

<pre><code>&gt;&gt;&gt; z = dict(list(x.items()) + list(y.items()))
&gt;&gt;&gt; z
{'a': 1, 'c': 11, 'b': 10}
</code></pre>
