To copy a dictionary in python, there are several approaches with different properties:
Copy a dictionary with deepcopy
To get a copy completely independent of the first dictionary a solution is to use the function deepcopy
>>> import copy
>>> d1 = {'a':1}
>>> d2 = copy.deepcopy(d1)
>>> d2
{'a': 1}
Here if the second dictionary d2 is modified the first dictionary will be not affected, examples:
>>> d2['a'] = 7
>>> d2
{'a': 7}
>>> d1
{'a': 1}
another example:
>>> d1 = {'a':[1,2,3,4]}
>>> d2 = copy.deepcopy(d1)
>>> d2['a'].append(5)
>>> d2
{'a': [1, 2, 3, 4, 5]}
>>> d1
{'a': [1, 2, 3, 4]}
Copy a dictionary with a for loop
To copy a dictionary it is also possible to use a for loop:
>>> d1 = {'a':1,'b':2}
>>> d2 = {}
>>> for key in d1:
... d2[key] = d1[key]
...
If the values associated with the dictionary keys are not iterable objects, then if one of the dictionary is modified the other one will be not, examples
>>> d2
{'a': 1, 'b': 2}
>>> d2['a'] = 7
>>> d2
{'a': 7, 'b': 2}
>>> d1
{'a': 1, 'b': 2}
WARNING: However if the values associated with the dictionary keys are iterable objects, such as lists for example, then if one of the dictionary is modified the other one will be too (it is called a shallow copy), examples:
>>> d1 = {'a':[1,2,3],'b':[10,11,12]}
>>> d2 = {}
>>> for key in d1:
... d2[key] = d1[key]
...
>>> d2
{'a': [1, 2, 3], 'b': [10, 11, 12]}
>>> d2['a'].append(4)
>>> d2
{'a': [1, 2, 3, 4], 'b': [10, 11, 12]}
>>> d1
{'a': [1, 2, 3, 4], 'b': [10, 11, 12]}
In the previous example to overcome this property a solution is to use the copy() function, example:
>>> d1 = {'a':[1,2,3],'b':[10,11,12]}
>>> d2 = {}
>>> for key in d1:
... d2[key] = d1[key].copy()
...
>>> d2
{'a': [1, 2, 3], 'b': [10, 11, 12]}
>>> d2['a'].append(4)
>>> d2
{'a': [1, 2, 3, 4], 'b': [10, 11, 12]}
>>> d1
{'a': [1, 2, 3], 'b': [10, 11, 12]}
Copy a dictionary with copy()
To copy a dictionary, another solution is to use the copy() function:
>>> d1 = {'a':1}
>>> d2 = d1.copy()
>>> d2
{'a': 1}
But this function is also a shallow copy and if the values associated with the dictionary keys are not iterable objects, then if one of the dictionary is modified the other one will be not, example
>>> d2['a']=7
>>> d2
{'a': 7}
>>> d1
{'a': 1}
However, if the values associated with the dictionary keys are iterable objects, then if one of the dictionary is modified the other one will be too, example
>>> d1 = {'a':[1,2,3]}
>>> d2 = d1.copy()
>>> d2
{'a': [1, 2, 3]}
>>> d2['a'].append(4)
>>> d2
{'a': [1, 2, 3, 4]}
>>> d1
{'a': [1, 2, 3, 4]}
Copy a dictionary with dict()
Same thing if the function dict() is used to copy a dictionary, examples:
>>> d1 = {'a':1}
>>> d2 = dict(d1)
>>> d2['a']=7
>>> d2
{'a': 7}
>>> d1
{'a': 1}
with iterable values:
>>> d1 = {'a':[1,2,3]}
>>> d2 = dict(d1)
>>> d2
{'a': [1, 2, 3]}
>>> d2['a'].append(4)
>>> d2
{'a': [1, 2, 3, 4]}
>>> d1
{'a': [1, 2, 3, 4]}
Copy a dictionary using the = operator
If the = operator is used, it is not really a copy, it is more like one dictionary with two names, if one dictionary is modified (add or change a key) the other one will be too, examples:
>>> d1 = {'a':1}
>>> d2 = d1
>>> d2
{'a': 1}
>>> d2['a'] = 7
>>> d2['c'] = 3
>>> d2
{'a': 7, 'c': 3}
>>> d1
{'a': 7, 'c': 3}
References
Links | Site |
---|---|
How to copy a dictionary and only edit the copy | stackoverflow |
copy — Shallow and deep copy operations | python doc |
Description | tutorialspoint |
Be careful with using dict() to create a copy | peterbe.com |