The discard method

The discard method removes elements that are in the set. If there is no element, then the original set will simply be returned. Unlike the remove method, which returns an error. In the method parameter, we pass the element we want to remove.

Syntax

pythonset.discard(what to delete)

Example

Let's remove the element 'a' from our set:

pythonst = {'a', 'b', 'c'}
st.discard('a')
print(st)

Result of code execution:

python{'b', 'c'}

Example

Now let's remove an element that is not in the set:

pythonst = {'a', 'b', 'c'}
st.discard('e')
print(st)

After executing the code, we will get back the original array:

python{'a', 'b', 'c'}

See also

  • method remove,
    which removes elements from a set
  • method pop,
    which removes the first element from a set
  • method clear,
    which clears the multitude
  • method add,
    which adds elements to a set
  • function len,
    which returns the length of the set
  • method union,
    which unites sets