510
Python Set remove() Method
Python remove() method removes an element elem from the set. It raises error KeyError if elem is not contained in the set. See the examples given below.
Signature
Parameters
elem: element to be deleted.
Return
It returns None but throws KeyError if the value does not found in the set.
Let’s see some examples of remove() method to understand it’s functionality.
Python Set remove() Method Example 1
Let’s first see a simple example to remove an element from the set.
Output:
{1, 2, 3} After removing element: {2, 3}
Python Set remove() Method Example 2
It throws an error KeyError if the element is not available in the set. See the example.
Output:
set.remove(22) KeyError: 22
Python Set remove() Method Example 3
This method can be easily implemented into program to perform some business logic. See an examplpe below.
Output:
{'a', 'c', 'i', 't', 'n', 'u', 'y', 's', 'd', 'o', 'r'} {'a', 'i', 'u', 'o'}
Next TopicPython Set