CKTIS / Python
set.remove
Usuwa wskazany element ze zbioru, wymagając jego obecności.
Wersja: Python 3.14; repository image, production runtime not attested
Składnia
s.remove(element, /)Usuwa wskazany element ze zbioru, wymagając jego obecności.
Pierwsze wywołanie znajduje 3, zwraca None i pozostawia {2}. Drugie zgłasza KeyError, ponieważ 3 już nie ma.
Gdy brak elementu jest dopuszczalny, można użyć discard.
s = {1, 2}
assert s.remove(2) is None and s == {1}
Przykłady
s = {2, 3}
s.remove(3)
try:
s.remove(3)
except KeyError as error:
print(type(error).__name__)
print(sorted(s))
Wynik
KeyError [2]
Gdy brak elementu jest dopuszczalny, można użyć discard.