Remove Element from Set Python || Sets || Python


In this we are going to see a basic program which removes elements from sets in Python Programming Language.


set1 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
print("Set : ", set1)

# Sets Are Mutable And Doesn't Have Indexing So Sometime It May Give Error At Removing Element
# For Removing Last Element
set1.pop()
print("-------------------------------------")
print("After Removing The Set Is : ", set1)

# For Removing Particular Element
# First Method
set1.remove(2)
print("-------------------------------------")
print("After Removing an element, the Set Is : ", set1)
# Second Method
set1.discard(3)
print("-------------------------------------")
print("After Removing an element, the Set Is : ", set1)

# For Removing All Elements
set1.clear()
print("-------------------------------------")
print("After Clearing, the Set Is : ", set1)

# Coded By DHIRAJ SHELKE


#ENJOY CODING


Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post