Google News
logo
Python Program to Remove an item in a set by using the discard() method
Certainly! Here's an example program that demonstrates how to remove an item in a set using the `discard()` method :
Program :
# Create a set
fruits = {"apple", "banana", "cherry"}

# Remove an item using the discard() method
fruits.discard("banana")

# Print the updated set
print(fruits)
Output :
{'cherry', 'apple'}
In the above program, we first create a set `fruits` containing three items: "apple", "banana", and "cherry". We then remove the item "banana" from the set using the `discard()` method.

Finally, we print the updated set to confirm that "banana" has been removed.