Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




PYTHON - REMOVE FROM SET


How to remove an element from the Set by its name/value?


Let us say, we have a Set that contains three names, 'Mohan', 'Kriti' and 'Salim'. And we want to remove 'Kriti' from the Set.


It can be done with the 'remove( )' and 'discard( )' Function.


There is a mild difference between 'remove( )' and 'discard( )' Function. Let us see them below.


At first let us look at the 'remove( )' Function.


Example :


x = {"Mohan", "Kriti", "Salim"}
x.remove("Kriti")
print(x) 


Output :



  {'Mohan', 'Salim'}

So, in the above code we have created a 'Set' and initialised to the variable 'x'.


x = {"Mohan", "Kriti", "Salim"}

java_Collections

Next, we have used the 'remove( )' Function that searches for the name 'Kriti' and removes it from the Set.


x.remove("Kriti")

java_Collections

And we get the below output,


{'Mohan', 'Salim'}

Now, if we replace 'remove( )' with 'discard( )' Function. It will also behave exactly as the 'remove( )' Function.


Example :


x = {"Mohan", "Kriti", "Salim"}
x.discard("Kriti")
print(x) 


Output :



  {'Mohan', 'Salim'}

The only difference between 'remove( )' and 'discard( )' Function is, if the element to be removed is not present in the Set, the 'remove( )' Function will raise an error. Whereas 'discard( )' Function will not raise an error.


Example :


x = {"Mohan", "Kriti", "Salim"}
x.remove("George")
print(x) 


Output :



   x.remove("George")
 KeyError: 'George'

So, we are trying to remove 'George' that is not present in the Set.


x = {"Mohan", "Kriti", "Salim"}

And the 'remove( )' Function throws an error.


   x.remove("George")
KeyError: 'George'

How to remove an element using the pop( ) Function?


The 'pop( )' Function can be used to remove an item from the Set. But it does not remove the last element.


As there is no indexing, the 'pop( )' Function could remove any element.


Example :


x = {"Mohan", "Kriti", "Salim"}
x.pop()
print(x) 


Output :



   {'Kriti', 'Salim'}

So, in the above code we have created a 'Set' and initialised to the variable 'x'.


x = ["Mohan", "Kriti", "Salim"]

Below is how the values are positioned in the Set,


java_Collections

Next, we have used the 'pop( )' function that can remove any element from the Set.


x.pop()

In this case 'Mohan' is removed from the Set.


java_Collections

And we get the below output,


{'Kriti', 'Salim'}

How to remove all the elements from the Set?


The 'clear( )' Function can be used to remove all the elements from the Set.


Example :


x = {"Mohan", "Kriti", "Salim"}
x.clear()
print(x) 


Output :



   set( )

So, in the above code we have created a 'Set' and initialised to the variable 'x'.


x = ["Mohan", "Kriti", "Salim"]

java_Collections

Next, we have used the 'clear( )' function that removes all the elements from the Set making the Set empty.


x.clear( )

And we get an empty Set as output,


set( )

The output 'set( )' means, it represents an empty Set.


How to delete a Set?


We have seen, the 'clear( )' Function can be used to remove all the elements from the Set.


Now, let us see, how can we delete a Set completely.


The 'del' keyword is used to remove the Set completely.


Example :


x = {"Mohan", "Kriti", "Salim"}
del x
print(x) 


Output :



   print(x)
 NameError: name 'x' is not defined

So, in the above code we have created a 'Set' and initialised to the variable 'x'.


x = ["Mohan", "Kriti", "Salim"]

java_Collections

Next, we have used the 'del' keyword to delete the Set completely.


del x

And when we try printing the Set,


print(x)

It ends up with an error.


 print(x)
NameError: name 'x' is not defined

As the set 'x' no longer exist.