how to kill dictionary entry

Lyle Johnson ljohnson at resgen.com
Thu May 10 18:26:36 EDT 2001


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

You need to delete the key entirely, using "del":

    >>> a = {}
    >>> a[1] = None
    >>> print a[1]
    None
    >>> del a[1]
    >>> print a[1]
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    KeyError: 1

Hope this helps,

Lyle





More information about the Python-list mailing list