having some pains with the following code in Python:
dict_items = sample_dict.items()
sample_dict.clear()
where sample_dict
is a dictionary from int
to dict
.
I am trying to save memory by clearing my dictionary (after this code is run I never use this dictionary to access values, I just care about the k,v
pairs stored in the dict), but after dict.clear()
is run, dict_items
gets nuked. Running print(dict_items)
yields the following:
[key1:{},key2:{}...]
Not very familiar with how memory is stored in python - how can I keep dict_items
intact after running sample_dict.clear()
?
CodePudding user response:
dict_item = [*sample_dict.items()]
CodePudding user response:
Just use the copy of the dictionary :
dict_items = sample_dict.copy().items()
sample_dict.clear()