Loading lesson path
To remove an item in a set, use the , or the discard() method.
Remove "banana" by using the
method:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)If the item to remove does not exist,
will raise an error.
Remove "banana" by using the discard() method:
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)If the item to remove does not exist, discard() will
raise an error.
method to remove an item, but this method will remove a random item, so you cannot be sure what item that gets removed.
method is the removed item.
Remove a random item by using the pop() method:
thisset = {"apple", "banana", "cherry"}
x =
thisset.pop()
print(x)
print(thisset)Sets are unordered, so when using the pop() method, you do not know which item that gets removed.
method empties the set:
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)The del keyword will delete the set completely:
thisset = {"apple", "banana", "cherry"}
del thisset print(thisset)