Python merge dictionaries with custom merge function

前端 未结 8 1948
北海茫月
北海茫月 2021-01-13 18:39

I want to merge two dictionaries A and B such that the result contains:

  • All pairs from A where key is unique to A
  • All pairs from B where key is unique
8条回答
  •  -上瘾入骨i
    2021-01-13 19:30

    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'}
    

提交回复
热议问题