Skip to main content

clear()

The clear() method in Python is a Set method that removes all the elements from a set. It empties the set, making it completely empty with a length of zero.

Parameter Values

This function does not accept any parameters.

Return Values

The clear() method from Set Methods in Python always returns None.

How to Use clear() in Python

Example 1:

The clear() method removes all elements from a set.

set1 = {1, 2, 3, 4, 5}
set1.clear()
print(set1)
Example 2:

Calling clear() on an empty set will not raise an error and will result in an empty set.

set2 = set()
set2.clear()
print(set2)
Example 3:

The clear() method is an in-place operation and does not return a new set.

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