Python Set - remove method

The remove() method removes the specified element from a Python set. If the specified element is not present in the set the remove() method raises a KeyError.

Set Updated 16 Sep 2016 1 min read

Method Signature

remove(element_in)

Method Overview

  • The method remove() of set class removes an element from an existing Python set object.
  • The method does not return a new set object. It returns None.

Parameters

element_in : The element that is to be removed from the Python set object.

Exceptions

KeyError - raised if the specified element is not in the Python set object. Note that the elements of a set are all keys and set is an associative container.

Example

python
# Example Python program that removes an element

            # from a Python set

            foodPlate = {"Salmon", "Beans", "Bacon", "Broccoli"}

            foodPlate.remove("Bacon")

            print(foodPlate)

Output

Output
{'Beans', 'Salmon', 'Broccoli'}