Appending a dictionary to a list in a a loop Python

后端 未结 5 1056
梦毁少年i
梦毁少年i 2020-11-29 23:42

I am a basic python programmer so hopefully the answer to my question will be easy. I am trying to take a dictionary and append it to a list. The dictionary then changes val

5条回答
  •  误落风尘
    2020-11-30 00:37

    Let's say d is your dictionary. Here, if you do d.copy(). It returns shallow copy that doesn't work when you have nested dictionary into d dictionary. To overcome from this issue, we have to use deepcopy.

    from copy import deepcopy
    list.append(deepcopy(d))
    

    It works perfectly !!!

提交回复
热议问题