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>Although there maybe a more efficient way to achieve what you want, i used the information <a href="https://docs.python.org/2/library/sets.html" rel="nofollow">here</a> to create the following functions:</p>

<pre><code>def f1(a,b):
    return a+b

def f2(a,b):
    return a&gt;b

def function2(d1,d2):
    out1 = {}
    out2 = {}
    #use the set operations to take the common keys
    commons = set(set(d1) &amp; set(d2))
    for i in commons:
        out1[i] = d1[i] &gt; d2[i]
    #all the non-common keys go to out2
    for i in d1:
        if i not in commons:
            out2[i] = d1[i]
    for i in d2:
        if i not in commons:
            out2[i] = d2[i]
    return (out1,out2)

def function1(d1,d2):
    out1 = {}
    out2 = {}
    #use the set operations to take the common keys
    commons = set(set(d1) &amp; set(d2))
    for i in commons: out1[i] = f1(d1[i],d2[i])
    #all the non-common keys go to out2
    for i in d1:
        if i not in commons:
            out2[i] = d1[i] 
    for i in d2:
        if i not in commons:
            out2[i] = d2[i]
    return (out1,out2) 

def main():

    d1 = {1:30, 2:20, 3:30, 5:80}
    d2 = {1:40, 2:50, 3:60, 4:70, 6:90}
    d1,d2 = function1(d1,d2)
    for i in d1:print(i,d1[i])
    print('\n')
    for i in d2:print(i,d2[i])

    print('\n\n')

    d1 = {1:30, 2:20, 3:30}
    d2 = {1:40, 2:50, 3:60}
    d1,d2 = function2(d1,d2)
    for i in d1:print(i,d1[i])
    print('\n')
    for i in d2:print(i,d2[i])



if __name__ == '__main__':
    main()
</code></pre>

<p>I tried to make my code as clear as possible.I hope it helps.</p>

python