Open In App

Python Set clear() Method

Last Updated : 03 Oct, 2025
Comments
Improve
Suggest changes
9 Likes
Like
Report

In Python, a set is a collection of unique, unordered elements. Sometimes, you may want to remove all elements from a set but still keep the set itself (not delete the variable).

It can be done using the clear() method in Python.

For Example: Consider a set s = {1, 2, 3, 4, 5, -1}

Input:
s = {1, 2, 3, 4, 5, -1}

Output:
s = {}

Here we have cleared all the values in the set but the set itself still exists.

Syntax:

set.clear()

  • Parameters: The clear() method doesn't take any parameters.
  • Return: None (the set is modified in place)

Let's look at the examples given below:

Example 1: Clearing a Set of Numbers

Python
num = {1, 2, 3, 4}
num.clear()
print("After clear() on test_set:", num)

Output
After clear() on test_set: set()

Here, all elements are removed and the set becomes an empty set.

Example 2: Clearing a Set of Strings

Python
# set of letters
GEEK = {"A", "B", "C"}
print('GEEK before clear:', GEEK)

GEEK.clear()
print('GEEK after clear:', GEEK)

Output
GEEK before clear: {'A', 'C', 'B'}
GEEK after clear: set()

When to Use clear()?

  • When you want to reuse a set variable without creating a new one.
  • When you want to reset a set during loops or iterations.
  • When you need to quickly empty data stored in a set.

Article Tags :

Explore