I want to merge two dictionaries A and B such that the result contains:
def merge_dict(dict1,dict2):
dict1={1:'red'}
dict2={2:'black',3:'yellow'}
dict1.update(dict2)
print 'dict3 =',dict1
merge_dict(dict1,dict2)
Output:
dict3 = {1: 'red', 2: 'black', 3: 'yellow'}
>>> def f(x,y):
... return x*y
...
>>> dict([(k,v) for k,v in A.items()] + [ (k,v) if k not in A else (k,f(A[k],B[k])) for k,v in B.items()])
{1: 1, 2: 6, 7: 3}