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?

- 73,364
- 6
- 83
- 116

- 75
- 2
- 5
-
5"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 container – juanpa.arrivillaga Mar 14 '21 at 07:03
-
2Depends 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. – rdas Mar 14 '21 at 07:04
-
1Perhaps a better way to phrase what I said above: exceptions exist to tell you something unexpected is happening. – juanpa.arrivillaga Mar 14 '21 at 07:10
-
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**. – Shah Vipul Mar 14 '21 at 07:17
-
@ShahVipul the OP already stated this... – juanpa.arrivillaga Mar 14 '21 at 07:18
-
4Exceptions 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. – user2357112 Mar 14 '21 at 07:19
-
@user2357112supportsMonica Ah I see I understand now. Thanks – TechSupportz Mar 15 '21 at 03:48
3 Answers
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.

- 2,912
- 2
- 25
- 45
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.

- 463
- 4
- 8
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()

- 2,540
- 1
- 7
- 24