Easy Tutorial
❮ Python Prime Number Python String Isalnum ❯

Python Set remove() Method

Python Set


Description

The remove() method is used to remove a specified element from the set.

This method differs from the discard() method because the remove() method raises an error if the element to be removed does not exist, whereas the discard() method does not.

Syntax

Syntax for the remove() method:

set.remove(item)

Parameters

Return Value

No return value.

Example

Remove the element "banana":

Example 1

fruits = {"apple", "banana", "cherry"}

fruits.remove("banana")

print(fruits)

Output result:

{'cherry', 'apple'}

Python Set

❮ Python Prime Number Python String Isalnum ❯