Python set clear

Python clear function is one of the set methods used to clear or remove total set items. In this section, we discuss how to use this function with practical examples. The basic syntax behind this set clear function is as:

set_Name.clear()

Python set clear Example

This method helps you to clear or remove the total number of items in a given set. The below code uses this method to remove items from a clearSet.

clearSet = {'apple', 'Mango', 'kiwi', 'orange', 'banana'}
print("\nOld Set Items = ", clearSet)

clearSet.clear()
print("After Clear Method = ", clearSet)
Python set clear Function Example

clear Example 2

clearIntSet = {15, 25, 35, 45, 55}
print("\nOld Items = ", clearIntSet)

clearIntSet.clear()
print("After = ", clearIntSet)
Old Items = {35, 45, 15, 55, 25}
After = set()

TIP: Please refer to the Python article to understand everything about Sets. For other methods, please refer to the set functions article.

After executing this clear method, it returns an empty = set().

mySet = {1, 2, 3, 4, 5, 6, 7, 8, 9}
print("Old Items = ", mySet)

mySet.clear()
print("New Items = ", mySet)

FruitSet = {'apple', 'Mango', 'orange', 'banana', 'cherry','kiwi'}
print("\nOld Items = ", FruitSet)

FruitSet.clear()
print("New Items = ", FruitSet)
Old Items = {1, 2, 3, 4, 5, 6, 7, 8, 9}
New Items = set()

Old Items = {'cherry', 'kiwi', 'Mango', 'banana', 'apple', 'orange'}
New Items = set()

Difference between Python set clear() and reassigning an empty set

If the task is to delete all items in a set that does not have any references to it, both the set.clear() function and reassigning to an empty set returns the same result. However, if there are any references, both behave differently.

In short, if you want the original set() and its references to be empty, use the clear() function. If the requirement is to delete the current set and keep the references as they are (unchanged), prefer reassigning an empty set option.

Using Python set clear() method

In the following example, we declared a set. Next, we used the assignment operator to refer to this set. Once we used the clear() method, both the original set (s) and their reference (a) become empty.

s = {10, 20, 30,40}
a = s
print(a)

s.clear()
print(s)
print(a)
{40, 10, 20, 30}
set()
set()

Reassigning to an empty set

In the following example, we reassigned the original set to an empty set using the set() function. Here, the original set becomes empty, whereas the reference set (a) still holds the values.

s = {10, 20, 30,40}
a = s
print(a)

s = set()
print(s)
print(a)
{40, 10, 20, 30}
set()
{40, 10, 20, 30} 

Python set clear vs discard vs remove

The set clear(), discard(), and remove() methods help to remove items from an existing set. However, their functionality differs.

  • clear() function removes all elements in a set at once. After implementing the set.clear() method, it returns an empty set.
  • discard() function deletes a specific element from a list. If the user-given element is not present in a set, it does nothing.
  • remove() function deletes a specific element from a list. If the user-given element is not present in a set, it raises KeyError.

Using clear()

As you can see, after implanting the set.clear() function, it returns an empty set.

s = {10, 20, 30,40}
s.clear()
print(s)
set()

Using set dicard()

When we try to remove a non-existing item (100) from a set, it does nothing and prints the result.

s = {10, 20, 30,40}
s.discard(20)
print(s)

s.discard(100)
print(s)
{40, 10, 30}
{40, 10, 30}

Using set remove()

When we try to delete an existing item, it simply removes that item from a set. However, when we try to remove 11, it raises KeyError. Also, refer to the set pop method

s = {10, 20, 30,40}
s.remove(30)
print(s)

s.remove(111)
print(s)
{40, 10, 20}
KeyError: 111