Python Set remove()

The remove() method searches for the given element in the set and removes it.

The syntax of remove() method is:

set.remove(element)

remove() Parameters

The remove() method takes a single element as an argument and removes it from the set.

If the element(argument) passed to the remove() method doesn't exist, keyError exception is thrown.


Return Value from remove()

The remove() method only removes the given element from the set. It doesn't return any value.


Example 1: Remove Element From The Set

# language set
language = {'English', 'French', 'German'}

# 'German' element is removed
language.remove('German')

# Updated language set
print('Updated language set: ', language)

When you run the program, the output will be:

Updated language set:  {'English', 'French'}

Example 2: Trying to Delete Element That Doesn't Exist

# animal set
animal = {'cat', 'dog', 'rabbit', 'guinea pig'}

# Deleting 'fish' element
animal.remove('fish')

# Updated animal
print('Updated animal set: ', animal)

When you run the program, you will get the following error:

Traceback (most recent call last):
  File "<stdin>", line 5, in <module>
    animal.remove('fish')
KeyError: 'fish'

It's because the element fish doesn't exist in the animal set.

You can use discard() method if you do not want this error. The set remains unchanged if the element passed to discard() method doesn't exist.


A set is an unordered collection of elements. If you need to remove arbitrary element from the set, you can use pop() method.