I want to copy a dictionary into a new variable without maintaining the link with previous variable.
from copy import deepcopy
new_dict = deepcopy(orig_dict)
dict.copy() creates shallow copies, which means, if your dictionary contains other container-objects like list, tuples, etc. they will be referenced again and not duplicated!
You can try for yourself with:
a = {1:{1:2}}
print id(a[1])
>>> 159584844
b = a.copy()
print id(b[1])
>>> 159584844
c = deepcopy(a)
print id(c[1])
>>> 159575276
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With