How to achieve deepcopy in dictionaries?
My original code :
li1 = ['Column_6', 'Column_11']
delimiters = ['a','b','c','d']
inner_dict = dict.fromkeys(delimiters,[0,0])
delim_dict = dict.fromkeys(li1 ,None)
for k,v in delim_dict.items():
delim_dict[k] = copy.deepcopy(inner_dict)
print (delim_dict) gives
{'Column_6': {'a': [0, 0], 'b': [0, 0], 'c': [0, 0], 'd': [0, 0]},
'Column_11': {'a': [0, 0], 'b': [0, 0], 'c': [0, 0], 'd': [0, 0]}}
delim_dict['Column_6']['a'][0]=5
print (delim_dict)
gives
{'Column_6': {'a': [5, 0], 'b': [5, 0], 'c': [5, 0], 'd': [5, 0]},
'Column_11': {'a': [0, 0], 'b': [0, 0], 'c': [0, 0], 'd': [0, 0]}}
Why keys b,c,d are updated with [5, 0] in spite of deepcopy?
I am able to achieve result with the Modified code :
li1 = ['Column_6', 'Column_11']
delimiters = ['a','b','c','d']
#inner_dict = dict.fromkeys(delimiters,[0,0])
delim_dict = dict.fromkeys(li1 ,None)
for k,v in delim_dict.items():
delim_dict[k] = {}
for delim in delimiters:
delim_dict[k][delim]=[0,0]
But, how can i achieve the same result with deep copy or is there any other efficient way?
Note:
I tried following this link:
Deep copy of a dict in python
No luck.
question from:
https://stackoverflow.com/questions/66066312/why-doesnt-deepcopy-work-on-this-dictionary