Updating nested dictionaries when data has existing key

前端 未结 5 709
感动是毒
感动是毒 2020-12-08 22:22

I am trying to update values in a nested dictionary, without over-writting previous entries when the key already exists. For example, I have a dictionary:

           


        
5条回答
  •  轮回少年
    2020-12-08 23:19

    You could treat the nested dict as immutable:

    myDict["myKey"] = dict(myDict["myKey"], **{ "nestedDictKey2" : anotherValue })

提交回复
热议问题