[Tutor] how best to remove an item from a dict

Alan Gauld alan.gauld at yahoo.co.uk
Mon Jan 6 20:33:14 EST 2020


On 07/01/2020 00:55, Alex Kleider wrote:
> 
> Which is best:
> 
>   >>>   _ = d.pop(key)
> or
>   >>>    del d[key]
> ??
> 
>   >>> import this
> ..
> There should be one-- and preferably only one --obvious way to do it.
> Although that way may not be obvious at first unless you're Dutch.

The second one is best, it clearly describes the intended effect.

The first one uses a side-effect of the pop/assignment operation,
its only when the _ changes value that the dict item goes out
of scope and is deleted.

A clearly defined operation is always better than a disguised one.
Especially from a maintenance point of view, and given maintenance
is the most expensive part of programming, that is critical.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list