YouTip LogoYouTip

Ref Set Discard

# Python3.x Python Set discard() Method [![Image 3: Python3 Lists](#) Python Sets](#) * * * ## Description The discard() method is used to remove a specified element from a set. This method differs from the `remove()` method because the `remove()` method raises an error if the element to be removed does not exist, whereas the `discard()` method does not. ## Syntax The syntax for the discard() method is: set.discard(value) ## Parameters * value -- Required, the element to be removed ## Return Value None. ## Example Remove the element 'banana' from the set: ## Example 1 fruits = {"apple", "banana", "cherry"} fruits.discard("banana")print(fruits) The output result is: {'cherry', 'apple'} [![Image 4: Python3 Lists](#) Python Sets](#)
← Ref Set Intersection_UpdateRef Set Difference β†’