32 of 151 menu

The clear method

The clear method removes all elements from the set. After using this method, the set will be empty. We do not pass anything in the method parameter.

Syntax

set.clear()

Example

Let's remove all elements from our set:

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

Result of code execution:

set()

See also

  • method remove,
    which removes elements from a set
  • method discard,
    which removes elements that are in the set
  • method pop,
    which removes the first element from a set
  • method add,
    which adds elements to a set
  • function len,
    which returns the length of the set
  • method union,
    which unites sets
byenru