Python Set remove() Method Tutorial

In this section, we will learn what the Set remove() method is and how to use it in Python.

What is Set remove() Method in Python?

The Python Set remove() method is used to remove an element from the target Set object.

Basically, this method takes one argument and that is the value we want it to be removed from a set object.

Note: there’s another method called discard() which does the same task. But the difference is that the remove() method will return an error if the specified argument does not exist in the target Set object while the discard() method does nothing!

Python Set remove() Method Syntax:

set.remove(value)

Set remove() Method Parameter:

The method takes one argument and that is the element we want it to be removed from the target Set object.

Set remove() method Return Value

The method does not return a value.

Example: using python set remove() method

set1 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}

set1.remove(15)

print(set1)

Output:

{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}

Leave a Reply