Why would someone use set.remove() instead of set.discard() [duplicate]
Asked Answered
K

3

8

I'm currently learning Python and I am just wondering in what situation one would use .remove() rather than .discard() when removing values from a set. Since .discard() does not raise an error when removing a element form a set if the element isn't present wouldn't it be the better one to use?

Kratz answered 14/3, 2021 at 7:1 Comment(7)
"Since .discard() does not raise an error when removing a element form a set if the element isn't present wouldn't it be the better one to use?" no, that doesn't follow at all. In general, you want things like that to raise an error. Only if you explicitly expect the item to possibly not be present and that doesn't make a difference would you want to use .discard. Exceptions exist to tell you something isn't right, and often (I would say usually) when you want to remove an element from a container, you are assuming it is in the containerHeadlight
Depends on your use case. Sometimes it might be fine to not raise any error, other times you might want the error so that you can do something else. .remove() is more general since you can always ignore the error in the first case.Violaviolable
Perhaps a better way to phrase what I said above: exceptions exist to tell you something unexpected is happening.Headlight
Discard() and remove() both removes the element from the set. If the element is not present in the set, then no error or exception is raised. If the element is not present in the set, then an error or exception is raised.Tachygraphy
@ShahVipul the OP already stated this...Headlight
Exceptions are like fire alarms - turning off the alarm won't put out the fire. If there's a fire, you want the alarm to go off. The problem is the fire, not the alarm.Misbehave
@user2357112supportsMonica Ah I see I understand now. ThanksKratz
M
17

Errors are raised to be caught and processed. They are not annoyances or hurdles. They are tools to identify conceptual errors, or to indicated unexpected behavior that one needs to pay attention to, or to deal with parts of the system that one does not have control over, or to use to control the flow of the code where the python doctrine says „fail rather than test“ i.e. let the code raise exceptions you expect rather than testing with if statements.

In the case of .discard() and .remove(): .discard() calls .remove() silently catches the exception in case the value was not there and silently returns. It’s a shortcut for a silent .remove(). It might be suitable for your special use-case. Other use-cases might require an exception to be raised when the value does not exist.

So .remove() is the general case that gives the developer control over the exception and .discard() is just a special use case where the developer does not need to catch that execration.

Midinette answered 14/3, 2021 at 7:14 Comment(1)
Your first paragraph is very well stated.Headlight
R
0

Like you have said .discard() method does not raise any error on the other hand .remove() method does. So it depends on the situation. You can use discard method if you do not want to catch the ValueError do to something else with try-except blocks.

Rasp answered 14/3, 2021 at 7:10 Comment(0)
Q
0

Well each of it has its own use in different cases.

For example you want to create a program that takes input from user and can add, remove or change data. Then when the user chooses the remove option and suppose it enters a value that is not in data, you would want to tell him that it is not in the data ( most probably using the try - except ), you would like to use the remove() function rather than .discard()

Quillon answered 14/3, 2021 at 7:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.