Set.clear() in Python is a built-in method for set objects that removes all elements from the set, leaving it empty. The method does not take any arguments and does not return any value.
Syntax
set.clear()
The clear() function does not have any parameters. But set_name is the name of your set.
Return Value
The method does not return any value, and it just returns None.
Example
# Declaring an empty set
vowels = set()
# Taking input from users
print("Enter all five vowels")
for i in range(5):
s = input()
vowels.add(s)
print("The set is: ", vowels)
# Clearing the set
vowels.clear()
print("After clearing the set is: ", vowels)
Output
Enter all five vowels
a
e
i
o
u
The set is: {'e', 'a', 'u', 'o', 'i'}
After clearing the set is: set()
In this example, we have taken the input of 5 vowels from the user in the set named vowels. Then we printed that set.
After that, we used a clear() method to remove all elements from the set, and finally, we can see that the set is empty.
To remove a specific element from the Set, you can use the “set.remove()” or the set.discard() method.