w3resource logo


Python Exercises

Python Exercise: Remove an item from a set if it is present in the set

Python sets: Exercise-5 with Solution

Write a Python program to remove an item from a set if it is present in the set.

Sample Solution :-

Python Code :

#Create a new set
num_set = set([0, 1, 2, 3, 4, 5])
#Discard number 4
num_set.discard(4)
print(num_set)

Output :

{0, 1, 2, 3, 5}

Python Code Editor:

#Create a new set
num_set = set([0, 1, 2, 3, 4, 5])
#Discard number 4
num_set.discard(4)
print(num_set)

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.