English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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() method takes a single element x and removes it from the set (if it exists).
If the x element exists, discard() removes it from the set.
This method returns None (which means, no return value).
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}
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}