How to copy a dictionary and only edit the copy

前端 未结 20 1979
说谎
说谎 2020-11-21 06:59

Can someone please explain this to me? This doesn\'t make any sense to me.

I copy a dictionary into another and edit the second and both are changed. Why is this hap

20条回答
  •  情深已故
    2020-11-21 07:45

    In Depth:

    Whenever you do dict2 = dict1, dict2 refers to dict1. Both dict1 and dict2 points to the same location in the memory. This is just a normal case while working with mutable objects in python. When you are working with mutable objects in python you must be careful as it is hard to debug.

    Instead of using dict2 = dict1, you should be using copy(shallow copy) and deepcopy method from python's copy module to separate dict2 from dict1.

    The correct way is:

    >>> dict1 = {"key1": "value1", "key2": "value2"}
    >>> dict2 = dict1.copy()
    >>> dict2
    {'key1': 'value1', 'key2': 'value2'}
    >>> dict2["key2"] = "WHY?"
    >>> dict2
    {'key1': 'value1', 'key2': 'WHY?'}
    >>> dict1
    {'key1': 'value1', 'key2': 'value2'}
    >>> id(dict1)
    140641178056312
    >>> id(dict2)
    140641176198960
    >>> 
    

    As you can see the id of both dict1 and dict2 are different, which means both are pointing/referencing to different locations in the memory.

    This solution works for dictionaries with immutable values, this is not the correct solution for those with mutable values.

    Eg:

    >>> import copy
    >>> dict1 = {"key1" : "value1", "key2": {"mutable": True}}
    >>> dict2 = dict1.copy()
    >>> dict2
    {'key1': 'value1', 'key2': {'mutable': True}}
    >>> dict2["key2"]["mutable"] = False
    >>> dict2
    {'key1': 'value1', 'key2': {'mutable': False}}
    >>> dict1
    {'key1': 'value1', 'key2': {'mutable': False}}
    >>> id(dict1)
    140641197660704
    >>> id(dict2)
    140641196407832
    >>> id(dict1["key2"])
    140641176198960
    >>> id(dict2["key2"])
    140641176198960
    

    You can see that even though we applied copy for dict1, the value of mutable is changed to false on both dict2 and dict1 even though we only change it on dict2. This is because we changed the value of a mutable dict part of the dict1. When we apply a copy on dict, it will only do a shallow copy which means it copies all the immutable values into a new dict and does not copy the mutable values but it will reference them.

    The ultimate solution is to do a deepycopy of dict1 to completely create a new dict with all the values copied, including mutable values.

    >>>import copy
    >>> dict1 = {"key1" : "value1", "key2": {"mutable": True}}
    >>> dict2 = copy.deepcopy(dict1)
    >>> dict2
    {'key1': 'value1', 'key2': {'mutable': True}}
    >>> id(dict1)
    140641196228824
    >>> id(dict2)
    140641197662072
    >>> id(dict1["key2"])
    140641178056312
    >>> id(dict2["key2"])
    140641197662000
    >>> dict2["key2"]["mutable"] = False
    >>> dict2
    {'key1': 'value1', 'key2': {'mutable': False}}
    >>> dict1
    {'key1': 'value1', 'key2': {'mutable': True}}
    

    As you can see, id's are different, it means that dict2 is completely a new dict with all the values in dict1.

    Deepcopy needs to be used if whenever you want to change any of the mutable values without affecting the original dict. If not you can use shallow copy. Deepcopy is slow as it works recursively to copy any nested values in the original dict and also takes extra memory.

提交回复
热议问题