How to copy a dict and modify it in one line of code

后端 未结 9 1009
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 00:50

Very often I need to create dicts that differ one from another by an item or two. Here is what I usually do:

setup1 = {\'param1\': val1, 
            \'param         


        
9条回答
  •  醉酒成梦
    2020-12-29 01:28

    I like this line (after from itertools import chain):

    d3 = dict(chain(d1.items(), d2.items()))
    

    (Thanks for juanpa.arrivillaga for the improvement!)

提交回复
热议问题