Raising the exception in one case is a pretty significant difference. If you are trying to remove an element from a set that is not there, there will be an error, it is better to use set.remove() rather than set.discard() .
Both methods are identical in implementation, except that, compared to set_discard() function adds strings:
if (rv == DISCARD_NOTFOUND) { set_key_error(key); return NULL; }
A KeyError occurs. Since this is a bit more work, set.remove() is the lower faction slower; Your processor must perform one additional test before returning. But if your algorithm is dependent on an exception, then an additional branching test is unlikely to matter.
source share