To determine how many items a set has, use the len()
method.
Get the number of items in a set:
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
To remove an item in a set, use the remove()
, or the discard()
method.
Remove "banana" by using the remove()
method:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
Note: If the item to remove does not exist, remove()
will raise an error.
Remove "banana" by using the discard()
method:
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)