Python Set clear() Method

Spread the love

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.

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()

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.

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

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()

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 !!

Malli

I am Mallikarjuna an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly documentation. Over the years, I have written hundreds of articles in Pandas, NumPy, Python, and I take pride in my ability to bridge the gap between technical experts and end-users by delivering well-structured, accessible, and informative content.

Leave a Reply

You are currently viewing Python Set clear() Method