Python Set clear()

Python Set clear() method removes all the elements from the set.

In this tutorial, we will learn the syntax of set.clear() method and go through example programs.

Syntax

The syntax of set.clear() is

</>
Copy
set.clear()

Examples

1. Remove all elements from set

In the following program, we will remove all the elements from the set s.

Python Program

</>
Copy
s = {'apple', 'banana', 'cherry'}
s.clear()
print('Resulting set :', s)

Program Output

Resulting set : set()

2. clear() method on empty set

In the following program, we will call clear() method on an empty set s.

Python Program

</>
Copy
s = set()
s.clear()
print('Resulting set :', s)

Program Output

Resulting set : set()

The empty set remains unchanged, since there are no elements to remove.

Conclusion

In this Python Tutorial, we learned about Python set method set.clear(). We have gone through the syntax of clear() method, and its usage with example programs.