how to kill dictionary entry
Jose' Sebrosa
sebrosa at artenumerica.com
Thu May 10 23:25:08 EDT 2001
dsavitsk wrote:
>
> i have a daemon process running that keeps track of connections to it in a
> dictionary. when a client disconnects i set the dictionary value = None,
> but the key persists. is there a way to get rid of it totally? for example,
> in the code below, i want a[1] and a[2] to throw the same error.
>
> >>> a = {}
> >>> a[1] = None
> >>> print a[1]
> None
> >>> print a[2]
> Traceback (innermost last):
> File "<interactive input>", line 1, in ?
> KeyError: 2
To remove the key 1 from the dict a, you want to do
del a[1]
instead of
a[1] = None
More: This gives you a KeyError if a as not 1 as key. To avoid that, you can do
if a.has_key(1): del a[1]
Check that in the methods of dictionaries.
Sebrosa
More information about the Python-list
mailing list