sum value of two different dictionaries which is having same key

前端 未结 3 488
既然无缘
既然无缘 2021-01-19 22:04

i am having two dictionaries

first = {\'id\': 1, \'age\': 23}
second = {\'id\': 4, \'out\': 100} 

I want output dictionary as

{\'id\': 5, \         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-19 22:47

    You can simply update the 'id' key afterwards:

    result = dict(first.items() + second.items())
    result['id'] = first['id'] + second['id']
    

提交回复
热议问题