set.discard() - Removes a single element from a set.

PHOTO EMBED

Tue Jun 18 2024 02:57:51 GMT+0000 (Coordinated Universal Time)

Saved by @pynerds #python

#unlike set.remove(), If the target element does not exist, set.discard method does not raise a KeyError, it returns None instead.

myset = {1, 3, 4, 5, 7, 8, 9}

#remove elements
myset.discard(4)
myset.discard(8)

print(myset)
content_copyCOPY

https://www.pynerds.com/set-discard-method-in-python/