The remove() function in Python is a method used with sets to remove a specific element. If the element is not present in the set, a KeyError will be raised.
Parameter Values
| Parameter | Description |
|---|---|
| value | The element to remove from the set. If the element is not present, it raises a KeyError. |
Return Values
The remove() method doesn't return any value; it returns None.
How to Use remove() in Python
Example 1:
The remove() method removes the specified element from a set.
my_set = {1, 2, 3, 4}
my_set.remove(3)
print(my_set)
Example 2:
If the element is not present in the set, a KeyError will be raised.
my_set = {1, 2, 3, 4}
my_set.remove(5)
print(my_set)
Example 3:
The remove() method modifies the original set and does not return any value.
my_set = {1, 2, 3, 4}
my_set.remove(2)
print(my_set)