The set.clear() method in Python is used to remove all elements or items from a set. It does not take any arguments and does not return any value. It simply removes all items from the set, leaving it empty. In this article, I will explain the syntax of the python set.clear() method, its parameters and explain how to remove all the elements from a given set with examples.
<a id="h-1-quick-examples-of-set-clear-method"></a>
1. Quick Examples of Set clear() Method
If you are in a hurry, below are some quick examples of the set clear() method.
# Below are the quick examples
# Example 1: Clear the sets of integers
# Using set clear() method
set_numbers = {2, 5, 8, 9, 4}
set_numbers.clear()
# Example 2: Delete the set
set_numbers = {2, 5, 8, 9, 4}
del set_numbers
# Example 3: Clear the sets of strings using set clear() method
technology = {'Java','Hadoop','Spark','Pandas','Pyspark','NumPy'}
technology.clear()
# Example 3: Clear an empty set
# Using set clear() method
set1 = {}
set1.clear()
<a id="h-2-syntax-of-set-clear-method"></a>
2. Syntax of Set clear() Method
Following is the syntax of clear() method
# Syntax of clear() function
set.clear()
2.1 Parameter of clear()
The clear() method does not take any parameters.
<a id="h-3-python-set-clear-examples"></a>
3. Python Set Clear Examples
You can clear or remove all elements from the python set using clear() method. You need to call this function on a set object from which you wanted to clear the elements. It does not take any arguments and does not return any value. In the below example, I have used notation {} to create a set. You can also use the set() to create a set.
# initialzing a set
set_numbers = {2, 5, 8, 9, 4}
print(set_numbers)
# Output
# {2, 4, 5, 8, 9}
# Clear the sets of integers
# Using set clear() method
set_numbers.clear()
print(set_numbers)
# Output
# set()
You can also use the python del statement to delete the set entirely. del is a keyword in Python that is used to delete an object. It can be used to delete variables, lists, dictionary elements, or object attributes.
# Delete the set
set_numbers = {2, 5, 8, 9, 4}
del set_numbers
print(set_numbers)
# Output
# NameError: name 'set_numbers' is not defined
<a id="h-4-clear-the-sets-of-strings-using-set-clear-method"></a>
4. Clear the Sets of Strings Using Set clear() Method
Let's see another example with a set of strings and clear all strings from the python set.
# Clear the sets of strings using set clear() method
technology = {'Java','Hadoop','Spark','Pandas','Pyspark','NumPy'}
technology.clear()
print(technology)
# Output
# set()
<a id="h-5-clear-an-empty-set-using-set-clear-method"></a>
5. Clear an Empty Set Using Set clear() Method
If you apply the clear() method on an empty set, the set will remain unchanged. For example,
# Clear an empty set
# Using set clear() method
set1 = {}
set1.clear()
print(set1)
# Output
# {}
6. Conclusion
In this article, I have explained the Python set clear() method syntax, parameters, and how to remove all the elements from a given set with examples. This method doesn't take any arguments and returns nothing but removes the elements from the set.
Happy Learning !!
<a id="h-related-articles"></a>
Related Articles
- How to initialize Set in Python?
- How to subtract two sets in Python?
- How to convert Python list to set?
- Python set comprehension examples
- Remove set element if exists in Python
- Find the length of the set in Python
- How to check two sets are equal in Python?
- How to convert set to string in Python?
- Join two or multiple sets in Python
- How to Append or Add Multiple Values to Set in Python?
- Python Set pop() method
- Python Set intersection() Explained
- Python Set remove() - Remove Element
