Python Set remove() Method

Python3 列表Python Sets


Description

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

This method is different from thediscard()method, because theremove()method raises an error when removing a non-existent element, while thediscard()method does not.

Syntax

The syntax of the remove() method is:

set.remove(item)

Parameters

  • item -- The element to be removed

Return Value

No return value.

Examples

Remove the element banana:

Example 1

fruits = {"apple", "banana", "cherry"} fruits.remove("banana") print(fruits)

The output result is:

{'cherry', 'apple'}

Python3 列表Python Sets

Other Extensions