English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python basic tutorial

Python flow control

Fonctions en Python

Types de données en Python

Python file operations

Python objects and classes

Python date and time

Advanced knowledge of Python

Python reference manual

Usage and examples of Python set discard()

Méthodes de collection en Python

Python set discard() is used to remove the specified element from the set (if it exists).

The syntax of discard() in Python is:

s.discard(x)

discard() parameters

discard() method takes a single element x and removes it from the set (if it exists).

discard() return value

If the x element exists, discard() removes it from the set.

This method returns None (which means, no return value).

Example1How does discard() work?

numbers = {2, 3, 4, 5}
numbers.discard(3)
print('numbers =', numbers)
numbers.discard(10)
print('numbers =', numbers)

When running this program, the output is:

numbers = {2, 4, 5}
numbers = {2, 4, 5}

Example2How does discard() work?

numbers = {2, 3, 5, 4}
# Returns None
# This means, no return value
print(numbers.discard(3))
print('numbers =', numbers)

When running this program, the output is:

None
numbers = {2, 4, 5}

Méthodes de collection en Python