Comparing 2 different dictionaries with random function
<p>So, what I am trying to achieve here is to take 2 dictionaries, take the values of both dictionaries in the same "spot" and be able to apply any function to it. Here's an example of some pseudo code:</p>

<pre><code>If f(a, b) returns a + b
d1 = {1:30, 2:20, 3:30, 5:80}
d2 = {1:40, 2:50, 3:60, 4:70, 6:90}
then function(d1, d2) returns ({1: 70, 2: 70, 3: 90}, {4: 70, 5: 80, 6: 90})
If f(a, b) returns a &gt; b
d1 = {1:30, 2:20, 3:30}
d2 = {1:40, 2:50, 3:60}
then function(d1, d2) returns ({1: False, 2: False, 3: False}, {})
</code></pre>

<p>Firstly find the intersection and the unions of the two dictionaries (as sets)
The intersections are used for the first item of the tuple
The differences are used for the second item of the tuple.</p>

<p>The functor is the operation to perform on dictionary items with keys from the intersection values. This is the final result used in the first tuple item.</p>

<p>To get the final results  for the second tuple, find the merged dictionary of d1 and d2, then return only the dictionary key values with keys from the differences</p>

<pre><code>def func(d1, d2, functor):
    s_intersection = set(d1) &amp; set(d2)
    s_difference = set(d1) ^ set(d2)
    merged = d1.copy()
    merged.update(d2)
    return {k: functor(d1[k], d2[k]) for k in s_intersection}, {k: merged[k] for k in s_difference}

d1 = {1:30, 2:20, 3:30, 5:80}
d2 = {1:40, 2:50, 3:60, 4:70, 6:90}
print func(d1, d2, lambda x, y: x+y)

d1 = {1:30, 2:20, 3:30}
d2 = {1:40, 2:50, 3:60}
print func(d1, d2, lambda x, y: x&gt;y)
</code></pre>

python