开发者

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

开发者 https://www.devze.com 2023-01-24 19:04 出处:网络
I use this way to remov开发者_C百科e an emelment from a dict: d[\"ele\"] = data ... d[\"ele\"] = None

I use this way to remov开发者_C百科e 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?


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}
>>> 


That does not remove the key, just the value.

del d['ele']
0

精彩评论

暂无评论...
验证码 换一张
取 消