Set clear() Method with examples
The clear() method in Python removes all the elements from a given Set.
Python Set clear() method Syntax
set.clear()
Parameter: None. This method doesn't take any parameters..
Return value: This method doesn't return any value, it just removes all the items from the set.
Python Set clear() method example
In the following example we have a set of numbers and we are calling clear() method to remove all the elements of this set.
# A Set of numbers
numbers = {5, 8, 10, 9, 1}
# Displaying set before executing clear()
print("Original Set is:", numbers)
# removing all the elements from the set
numbers.clear()
# displaying set after calling clear()
print("Updated Set is:", numbers)
As you can see in the output that there are no elements in the set after we have called the clear() method. If you want to remove a specific element from the Set then using remove() or discard() method.
Leave Comment