Python Set clear() Method

set.clear() removes all items from a set.

Use it when you want to empty a set without changing the variable itself. This method changes the existing set in place, which is important when the same set is used in more than one place.

numbers = {1, 2, 3}
numbers.clear()
print(numbers)

Output:

set()

clear() removes all items from the set in place. It does not create a new set.

What set.clear() does

The clear() method:

  • Removes every item from a set
  • Changes the original set in place
  • Returns None
  • Works on mutable set objects

If you are new to sets, see Python sets explained first.

Basic syntax

Syntax:

my_set.clear()

Key points:

  • It takes no arguments
  • It empties the set
  • It keeps the same set variable
  • Use it when you want the set to stay the same object, but have no items in it

Simple example

Here is a basic example:

colors = {"red", "green", "blue"}

colors.clear()

print(colors)

Output:

set()

What happens here:

  • colors starts with three values
  • colors.clear() removes them all
  • Printing the set shows set(), which means an empty set

clear() vs creating a new empty set

clear() and set() can both give you an empty set, but they do not work the same way.

Using clear()

items = {1, 2, 3}
other_name = items

items.clear()

print(items)
print(other_name)

Output:

set()
set()

Both variables now show an empty set because they refer to the same set object.

Using set()

items = {1, 2, 3}
other_name = items

items = set()

print(items)
print(other_name)

Output:

set()
{1, 2, 3}

Here, items = set() creates a new empty set object and assigns it to items.
But other_name still points to the original set.

This difference matters when more than one variable refers to the same set.

If you need help creating sets correctly, see how to create a set in Python.

Return value

clear() does not return the emptied set.

It returns None.

numbers = {1, 2, 3}

result = numbers.clear()

print(result)
print(numbers)

Output:

None
set()

Do not do this:

numbers = {1, 2, 3}
numbers = numbers.clear()

print(numbers)

Output:

None

This happens because numbers.clear() returns None, and then you assign that None back to numbers.

Common beginner mistakes

Here are the most common problems beginners run into with set.clear().

Assigning the result of clear()

Wrong:

my_set = {1, 2, 3}
my_set = my_set.clear()
print(my_set)

Output:

None

Correct:

my_set = {1, 2, 3}
my_set.clear()
print(my_set)

Output:

set()

Expecting clear() to remove only one item

clear() removes all items from the set.

If you want to remove a single value, use methods like remove() or pop().

Using {} for an empty set

This is a very common mistake:

empty_value = {}
print(type(empty_value))

Output:

<class 'dict'>

{} creates an empty dictionary, not an empty set.

To create an empty set, use:

empty_set = set()
print(type(empty_set))

Output:

<class 'set'>

Not realizing another variable points to the same set

a = {"x", "y"}
b = a

a.clear()

print(a)
print(b)

Output:

set()
set()

Because a and b refer to the same set, clearing one affects both.

FAQ

What does set.clear() return in Python?

It returns None. It empties the set in place.

Does clear() delete the set variable?

No. The variable still exists, but the set becomes empty.

How do I create an empty set after clearing?

You already have one after clear(). Printing it shows set().

What is the difference between clear() and set()?

clear() empties the existing set object. set() creates a new empty set object.

See also