merging Python dictionaries

前端 未结 4 2015
时光说笑
时光说笑 2020-11-27 04:40

I am trying to merge the following python dictionaries as follow:

dict1= {\'paul\':100, \'john\':80, \'ted\':34, \'herve\':10}
dict2 = {\'paul\':\'a\', \'joh         


        
4条回答
  •  情深已故
    2020-11-27 05:31

    This will work:

    {k: [dict1.get(k), dict2.get(k)] for k in set(dict1.keys() + dict2.keys())}
    

    Output:

    {'john': [80, 'b'], 'paul': [100, 'a'], 'peter': [None, 'd'], 'ted': [34, 'c'], 'herve': [10, None]}
    

提交回复
热议问题