In this Python tutorial, we ‘ll explain how the Python set clear method works. We will utilize concrete examples to gain a strong understanding of this method.
Outline
- Introduction: Python Set Clear Method
- Syntax of Clear Method
- Understanding Python Set Clear Method (Multiple Examples)
- Example 1: Clear Python Set
- Example 2: Return Value of Clear Method
- Example 3: Clear a List Of Sets Using Python For Loop
- Conclusion
Introduction: Python Set Clear Method
This method is used to remove all the items from Python set. It means that the specified Python set will be empty after this method is applied on it.
Let’s first see its syntax and after that, we’ll go through multiple easy examples for detailed understanding of how to properly use this method.
Syntax of Clear Method
set.clear()
- This method does not take any arguments as no parameters are specified in it.
- None is returned by this method. We’ll use a practical example to demonstrate it in detail.
Understanding Python Set Clear Method (Multiple Examples)
Below examples will make us understand how to properly make use of clear method.
Example 1: Clear Python Set
sVal={'a','b','c','d'} print('Set before applying clear method: ',sVal) sVal.clear() print('Set after applying clear method: ',sVal)
Output
Set before applying clear method: {'d', 'b', 'a', 'c'} Set after applying clear method: set()
- We’ve created a Python set.
- After that, we’ve applied the clear method on it.
- Finally, we’ve printed it and it displayed set() which means an empty set. We’ve to remember that empty {} will be taken as a dictionary by Python.
See below examples as well:
a={1,2,3,5} b={(2,3,4),('a','b','c'),(2.4,8.3)} c={9,8,9,(3,2,4),('a',4.7)} d={2,5,(8,7,9),('a','b',(1,2)),2.1,5.4} a.clear() b.clear() c.clear() d.clear() print('Set A cleared: ',a) print('Set B cleared: ',b) print('Set C cleared: ',c) print('Set D cleared: ',d)
Output
Set A cleared: set() Set B cleared: set() Set C cleared: set() Set D cleared: set()
We can see that by using Python set clear method, we can easily remove all the items of set whether it consists of items of same or different datatypes.
Example 2: Return Value of Clear Method
Let’s now see what is returned by Python set clear method. See below example.
aa={'aa','bb','cc',4.556,3,334} returnVal=aa.clear() print('The value returned by clear method is: ',returnVal)
Output
The value returned by clear method is: None
- First we’ve created a Python set.
- Then we’ve applied clear method on it and have stored the returned value in a variable.
- Finally, we’ve displayed value of this variable using Python print function.
- We can see that it shows None which means that the job of clear method is to only make the set empty. It does not return anything.
Example 3: Clear a List Of Sets Using Python For Loop
lstSets=[{'xyz','abc','efg'},{4,5,6,66},{5.4,3.2,9.8},{'aaab',556,4.442}] print('List of sets before applying clear method') print(lstSets) print('Applying for loop to clear each item of list(set in this case)') for var in lstSets: var.clear() print('List of sets after applying clear method') print(lstSets)
Output
List of sets before applying clear method [{'abc', 'xyz', 'efg'}, {66, 4, 5, 6}, {9.8, 3.2, 5.4}, {4.442, 556, 'aaab'}] Applying for loop to clear each item of list(set in this case) List of sets after applying clear method [set(), set(), set(), set()]
- First we’ve created a list of Python sets.
- After that, we’ve created a for loop which will take each item of list(these items are sets in our case) and apply clear method on it.
- Finally, we’ve printed this list using Python print function. As a result, all the items of list shows set() which means an empty set.
So this is how we can easily use Python set clear method. Do ask if you still have any queries regarding the proper usage of Python set clear method. We’ll be delighted to answer all.
Also, don’t forget to share it with other Python developers if you have learned something new from this tutorial.
Conclusion
To conclude this Python tutorial, we now have a practical knowledge of Python set clear method. We’d be very delighted to receive your valuable feedback on this post.
You can discover more concepts about Python programming by visiting our other posts. Thank you for reading this article.