开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜