Deleting An Optional Dict Entry (Posting On Python-List Prohibited)
Steve D'Aprano
steve+python at pearwood.info
Sat Jun 24 02:20:38 EDT 2017
On Fri, 23 Jun 2017 07:19 pm, Lawrence D’Oliveiro wrote:
> The construct
>
> del «dict»[«key»]
>
> deletes the entry with key «key» from the dictionary «dict», provided it
> exists. If it does not, you get a KeyError.
>
> To delete an entry which might or might not exist, the simplest way is this:
>
> «dict».pop(«key», None)
>
> Actually, the second argument to the pop method doesn’t matter, since the
> result from the call is ignored in this case.
Nice hint.
--
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
More information about the Python-list
mailing list