Click for All Topics
Syntax –
set_name.clear()
Example 1- let’s see the example
my_set={9,99,999,9999,99999}
print("my_set before clear:",my_set)
my_set.clear()
print("my_set after clear:",my_set)
# Output -
# my_set before clear: {99999, 99, 999, 9, 9999}
# my_set after clear: set()
Example 2- trying to give an argument to the clear method
s={'This', 'is', 'edSlash'}
print(s)
s.clear('is')
# Output -
# {'This', 'is', 'edSlash'}
# Traceback (most recent call last):
# File "/home/main.py", line 3, in
# s.clear('is')
# TypeError: set.clear() takes no arguments (1 given)
In the above example, we can see that we can’t give any argument to the clear method.
Example 3- Let’s see the return value of clear
s={'Sanskrit','Hindi','English','Urdu'}
print(s)
return_value=s.clear()
print(s)
print(return_value)
# Output -
# {'Sanskrit', 'Hindi', 'Urdu', 'English'}
# set()
# None
In the above example, we can see that the return value of the clear method is None.
Example 4- Run clear method on an empty set
my_set=set()
print(my_set)
my_set.clear()
print(my_set)
# Output -
# set()
# set()
Example 5-
my_list=[11,10,11,15,18,12,10]
my_set=set(my_list)
print('set before clear',my_set)
my_set.clear()
print('set after clear:',my_set)
# Output -
# set before clear {10, 11, 12, 15, 18}
# set after clear: set()
Office:- 660, Sector 14A, Vasundhara, Ghaziabad, Uttar Pradesh - 201012, India