Python -Remove Set Items
Remove Item
To remove an item in a set, use theremove(), or thediscard() method.
Example
Remove "banana" by using theremove() method:
thisset.remove("banana")
print(thisset)
Note: If the item to remove does not exist,remove() will raise an error.
Example
Remove "banana" by using thediscard() method:
thisset.discard("banana")
print(thisset)
Note: If the item to remove does not exist,discard() willNOT raise an error.
You can also use thepop() method to remove an item, but this method will remove a random item, so you cannot be sure what item that gets removed.
The return value of thepop() method is the removed item.
Example
Remove a random item by using thepop() method:
x = thisset.pop()
print(x)
print(thisset)
Note: Sets areunordered, so when using thepop() method, you do not know which item that gets removed.
Example
Theclear() method empties the set:
thisset.clear()
print(thisset)
Example
Thedel keyword will delete the set completely:
del thisset
print(thisset)

