Stealing this (A.get(k, B.get(k))
snippet from @MartijnPieters
>>> def f(x, y):
return x * y
>>> A = {1:1, 2:3}
>>> B = {7:3, 2:2}
>>> {k: f(A[k], B[k]) if k in A and k in B else A.get(k, B.get(k))
for k in A.viewkeys() | B.viewkeys()}
{1: 1, 2: 6, 7: 3}