In the ref I was given a solution for my problem, which while usable, is not thematically correct.
.pop() is for removing and retrieving an existing item (so a .get() and ‘del item’ combined). At no point did I look further into .pop() as it’s job is not to delete. It turns out it does have a ‘default’ so it can be used (abused ??) for this purpose.
Discoverability is one reason. That dict.pop takes a optional default is easy to miss. set.pop and list.pop don’t, but there’s set.discard. If you know about set.discard then you’ll naturally look for a dict equivalent by the same name.
That’s what I did anyway. I’ve written plenty of try/del/except KeyError’s, it never occuring to me that dict.pop could serve the same purpose.
try/del/except is a perfectly cromulent idiom, and having now learned it can be done with .pop(), you can choose to use that in the future. Wishing you’d known earlier might be an argument for proposing an improvement to the documentation, but not for adding redundant language feature.
I’m not sure I wish I’d learnt earlier. Using a function that returns a value for its side-effect, and then ignoring the value returned, is something I like to avoid. And you have to supply an expression which is ignored: Writing the statement aDict.pop(key, 42 .bit_count()) has the same effect as aDict.pop(key, None) (if a bit slower). Arguably, try/del/except is cleaner code.
Writing your own discard function is pretty easy, and a perfectly reasonable alternative if your main concern is a dislike of the style aspects of the standard approaches.
OK, “existing” rather than “standard”. But I was specifically responding to your comment that you disliked using pop. If you’re happy using try/del/except then that’s fine.
Well, this is now an aesthetic argument, not one about discoverability for people familiar with sets. That’s all well and good, but adding a fifth way to do it also has a counterargument grounded in aesthetics.
Well, that “side-effect” is the first effect mentioned in its doc: “If key is in the dictionary, remove it and return its value, else return default.” And the name suggests it, too. If I didn’t know what it does, then just from the name “pop”, I’d more likely guess that it removes the item and doesn’t return it than the other way around. Like in C++, for example.
I think you’re looking at this function upside down. Its intended use case is to remove an item. It’s not a getter. It is a mutate operation. It just happens to also return the removed value because that’s how you can interrogate exactly what it did after the fact. If you don’t care, then don’t use the result.
I’ve removed some replies that were getting off topic by being extremely pedantic about an unrelated side topic. As users, please be aware not to start that in the first place, but also not to continue replying to it if you see it. It makes topics difficult to follow or participate in. Start a new topic if you want to discuss something new. Flag posts that go off topic to be moved.