There are three ways to remove an element from a set in Python:
remove
method: This will remove the specified element from the set. If the element is not present in the set, it will raise a KeyError
.main.py80 chars4 lines
discard
method: This will also remove the specified element from the set. If the element is not present in the set, it will not raise any error.main.py81 chars4 lines
pop
method: This will remove and return an arbitrary element from the set. If the set is empty, it will raise a KeyError
.main.py76 chars4 lines
Note that since sets are unordered, the element removed using the pop
method is arbitrary and may differ between successive calls. Therefore, it is usually used when the specific element to remove is not important.
gistlibby LogSnag