Intersection and Difference of two dictionaries
<p>Given two dictionaries, I want to look at their intersction and difference and perform f function on the elements that intersect and perform g on the unique elements, Here's how I found out what the unique and intersecting elements are where d1 and d2 are two dictionaries, How do i print out the d_intersection and d_difference as dictionaries inside a tuple?  The output should look something like this ({intersecting keys,values}, {difference keys,values})
for example: given </p>

<pre><code>d1 = {1:30, 2:20, 3:30, 5:80}

d2 = {1:40, 2:50, 3:60, 4:70, 6:90}
</code></pre>

<p>The output should be <code>({1: 70, 2: 70, 3: 90}, {4: 70, 5: 80, 6: 90})</code></p>

<pre><code>dic = {}
d_intersect = set(d1) &amp; set(d2)
d_difference =  set(d1) ^ set(d2)
for i in d_intersect:
    dic.update({i : f(d1[i],d2[i])})
for j in d_difference:
    dic.update({j : g(d1[j],d2[j])})
</code></pre>

<p>Can someone tell me where I was going wrong and why does my code give key error 4?</p>

<p>You get a <em>KeyError</em> for <em>4</em> as <code>^</code> looks for the <em>symmetric difference</em> which means keys <em>unique to either</em>, the keys are <em>not in</em> both. You also don't need to create sets, you can use the <a href="https://docs.python.org/3/library/stdtypes.html#dictionary-view-objects" rel="nofollow">view object</a> returned from calling .keys</p>

<pre><code>d1 = {1: 30, 2: 20, 3: 30, 5: 80}

d2 = {1: 40, 2: 50, 3: 60, 4: 70, 6: 90}

 # d1.keys() ^ d2 -&gt; {4, 5, 6}, 4, 6 unique to d2, 5 unique to d1.
symm = {k: d1.get(k, d2.get(k)) for k in d1.keys() ^ d2}
inter = {k: d2[k] + d1[k] for k in d1.keys() &amp; d2}
</code></pre>

<p><code>d1.get(k, d2.get(k))</code> works for the <em>symmetric difference</em> as it catches when we get a unique  key from <code>d2</code>.</p>

<p>The code for python2 is slightly different, you would need to replace <code>.keys</code> with <code>.viewkeys</code> to get a <a href="https://docs.python.org/2/library/stdtypes.html#dictionary-view-objects" rel="nofollow"><em>view object</em>:</a></p>

<pre><code> {k: d1.get(k, d2.get(k)) for k in d1.viewkeys() ^ d2}
 {k: d2[k] + d1[k]  for k in d1.viewkeys() &amp; d2}
</code></pre>

<p>To get the just the difference between two sets i.e what is in a but not in b, you need <code>-</code>:</p>

<pre><code>In [1]: d1 = {1: 30, 2: 20, 3: 30, 5: 80}

In [2]: d2 = {1: 40, 2: 50, 3: 60, 4: 70, 6: 90}

In [3]: {k: d2[k] for k in d2.keys() - d1}
Out[3]: {4: 70, 6: 90}

In [4]: {k: d1[k] for k in d1.keys() - d2}
Out[4]: {5: 80}
In [5]: d2.keys() - d1 # in d2 not in d1
Out[5]: {4, 6}

In [6]: d1.keys() - d2 # in d1 not in d2
Out[6]: {5}

In [7]: d1.keys() ^ d2 # unique to either
Out[7]: {4, 5, 6}
</code></pre>

<p>The symmetric difference is like doing the union of the differences:</p>

<pre><code>In [12]: d1.keys() - d2 |  d2.keys() - d1
Out[12]: {4, 5, 6}
</code></pre>

<p>All the operators are discussed in the python <a href="https://docs.python.org/2/library/sets.html#set-objects" rel="nofollow">docs</a>, also the wiki page on <a href="https://en.wikipedia.org/wiki/Set_(mathematics)" rel="nofollow">Set_(mathematics)</a> gives you a good overview.</p>

python dictionary