Python Set clear()

The clear() method removes all items from the set.

Example


clear() Syntax

The syntax of the clear() method is:

 
set.clear()

Here, set.clear() clears the set by removing all the elements of the set.


clear() Parameters

The clear() method doesn't take any parameters.


clear() Return Value

The clear() method doesn't return any value.


Example 1: Python Set clear()

Output

Vowels (before clear): {'e', 'a', 'o', 'u', 'i'}
Vowels (after clear): set()

In the above example, we have used the clear() method to remove all the elements of the set vowels.

Here, once clearing the element, we get set() as output, which represents the empty set.


Example 2: clear() Method with a String Set

Output

Strings (before clear): {'Charlie', 'Mary', 'John'}
Strings (after clear): set()

In the above example, we have used the clear() method to remove all the elements of the set names.