Python Set – clear() method
clear() method of Python Set class clears all the elements in this set.
clear() method returns None. It modifies the original set.
In this tutorial, we will learn how to use Python set clear() method, with different usage scenarios explained with example programs.
Example 1: Clear Python Set
In this example, we shall take a Python set with some initial elements in it. Then, we use clear method on this set, and observe the resulting set.
Python Program
#initialize set
set_1 = {'a', 'b', 'c'}
#clear elements of set
set_1.clear()
print(set_1)
Run Output
set()
The resulting set is empty. clear() modifies the original set.
Example 2: Clear an empty Set
If you apply clear() method on an empty set, the empty set should be unchanged.
Python Program
#initialize set
set_1 = {}
#clear empty set
set_1.clear()
print(set_1)
Run Output
{}
Summary
In this tutorial of Python Examples, we learned how to use clear() method of Python set class to clear elements of a set, with the help of well detailed examples.