Merge two dictionaries
<p>I have something like this: </p>

<pre><code>Dict1 = {'a': "blabla", 'b': "gugu"}
Dict2 = {'a': "tadaa", 'b': "duduu"}
</code></pre>

<p>what I want to have is:</p>

<pre><code>Dict3 = {'tadaa': "blabla", 'duduu': "gugu"}
</code></pre>

<p>To some extent, this depends on what the edge cases are, and what you want to do about them (for example, what if the keys in <code>Dict1</code> and <code>Dict2</code> can be different?</p>

<p>Here's a solution that discards keys which only occur in one of the two dicts:</p>

<pre><code>&gt;&gt;&gt; Dict1 = {'a': "blabla", 'b': "gugu", 'x': 'nope'}
&gt;&gt;&gt; Dict2 = {'a': "tadaa", 'b': "duduu", 'y': 'nuh-uh'}

&gt;&gt;&gt; {Dict2[k]: Dict1[k] for k in set(Dict1) &amp; set(Dict2)}
{'tadaa': 'blabla', 'duduu': 'gugu'}
</code></pre>

python python-2.7 python-3.x