Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: Is this a wrong way to remove an element from a dict?

Tags:

python

I use this way to remove an emelment from a dict:

    d["ele"] = data
...
    d["ele"] = None

I think by this I can remove the reference on the original element so that the removed data can be freed, no memory leak.

Is it the right way to do this?

like image 237
Bin Chen Avatar asked Dec 10 '22 12:12

Bin Chen


1 Answers

You remove an element from a dictionary using del:

>>> d={}
>>> d['asdf']=3
>>> d['ele']=90
>>> d
{'asdf': 3, 'ele': 90}
>>> d['ele']=None
>>> d
{'asdf': 3, 'ele': None}
>>> del d['ele']
>>> d
{'asdf': 3}
>>> 
like image 179
Muhammad Alkarouri Avatar answered Mar 05 '23 02:03

Muhammad Alkarouri