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

Alex Kleider akleider at sonic.net
Tue Jan 7 02:20:45 EST 2020


On 2020-01-06 17:33, Alan Gauld via Tutor wrote:
> On 07/01/2020 00:55, Alex Kleider wrote:
>> 
>> Which is best:
>> 
>>   >>>   _ = d.pop(key)
>> or
>>   >>>    del d[key]
>> ??


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

How obvious it seems now that you've explained it!
Thanks, Alan


More information about the Tutor mailing list