Sets: .discard vs .remove

Learning about sets this week! I’m wondering when you might use .remove as opposed to .discard. From my understanding, the only difference is that .remove will raise an exception if an element that you’re trying to remove is not in the set. Presumably, wouldn’t you first want to search through a set to locate something prior to removing it?

if "word" in setName :

You would use set.discard when you don’t care whether the element was in
the set or not. Think of discard as being “if it is there, remove it,
otherwise don’t bother me”.

If it is not an error to remove something that’s not there, then use

You would use set.remove when you do care that the element is there
before removing it, or if it is an error to try to remove something that
isn’t there.

Analogy: if the governor says “remove Roger from prison!” but Roger is
nowhere to be found, then there’s a problem that needs to be addressed.

In this case, you have two choices:

(1) Look Before You Leap (LBYL): check whether the element is in the
set, and if so, remove it:

if roger in prison:

(2) Easier to Ask for Forgiveness than Pemission (EAFP): just remove it,
and if an exception occurs, catch it and deal with it (possibly by
ignoring it).

except KeyError:

Each choice has pros and cons. If missing elements are rare, then option
2 will be faster. If they are common, then option 1 will be faster. If
you are using threaded code or other parallel processing where more than
one piece of code has access to the set, then option 1 is unsafe and
option 2 is what you need. Otherwise its just a matter of what you

1 Like

Why? Sometimes you just want to ensure it isn’t there.

# uninteresting words for indexing
stop_words = set('and', 'or', 'its')
words = line.split()
index_words = words - stop_words

Now, thay is set subtraction but the same applies when you have a single
thing to remove, which happens. You only want to know it is gone, not
test ahead of time whether it is there first.

And conversely, sometimes you expect to be removing something which is
there, and if it isn’t something’s wrong with your code or your data and
you want an exception.

There are common use cases for both cases.

Cameron Simpson

1 Like

I have to say, you give some of the best explanations on this site. Thanks for the help. Makes sense to know that sometimes I have to figure out why something is missing (like Roger, for example).