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




PYTHON - INSERT IN SET


How to insert a new Item in a Set ?


Let us say, we have a Set that contains three names, 'Mohan', 'Kriti' and 'Salim'. And we want to insert a new name 'Nikhil'. But we actually don't know where 'Nikhil' would be inserted in the 'Set'. As the order is not maintained.


We can achieve that using the 'add( )' Function.


Example :


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


Output :



  {'Salim', 'Kriti', 'Nikhil', 'Mohan'}

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 'add( )' Function to add 'Nikhil' to the Set.


x.add("Nikhil")

And 'Nikhil' gets inserted at any location. And that's decided by Python.


java_Collections

And we get the below output,


{'Salim', 'Kriti', 'Nikhil', 'Mohan'}

Duplicates are not allowed in Set


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


As we have seen, we can use 'add( )' Function to achieve the above.


Example :


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


Output :



  {'Mohan', 'Kriti', 'Salim'}

And if you see the below output. 'Kriti' is not added twice. But 'Kriti' is present once.


That's because duplicates are not allowed in Set.


How to add more than one element in a Set ?


Let us say, we have a Set that contains three names, 'Mohan', 'Kriti' and 'Salim'. And we want to insert three names 'Sia','Andrew' and 'Kriti' in the Set.


And we need to create a new Set that will have the new names, 'Sia', 'Andrew' and 'Kriti'.And merge this new Set with the main Set.


There are three Functions that will help us achieve the same:


  1. 'update( )' Function that keeps the values from both sides.

  2. 'intersection_update( )' Function that keeps the values that are there in both Sets.

  3. 'symmetric_difference_update( )' that keeps the values that are not present in both Sets.

Let us see the 'update( )' Function first.


Example :


x = {"Mohan", "Kriti", "Salim"}
y = {"Sia", "Andrew", "Kriti"}
x.update(y)
print(x) 


Output :



  {'Mohan', 'Sia', 'Kriti', 'Salim', 'Andrew'}

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


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

java_Collections

Then what we have done is, put the names, 'Sia', 'Andrew' and 'Kriti' in another set 'y'.


y = {"Sia", "Andrew", "Kriti"}

java_Collections

And used the 'update( )' Function to add the new Set 'y'(With values 'Sia', 'Andrew' and 'Kriti') to the existing Set 'x'.


x.update(y)

And the new values, 'Sia' and 'Andrew' from Set 'y' gets added to the existing Set 'x'.


And since, 'Kriti' is present in both the Sets, 'Kriti' will be added once. Else it might cause a duplicate entry.


java_Collections

And we get the below output,


{'Mohan', 'Sia', 'Kriti', 'Salim', 'Andrew'}

So, as said, 'update( )' Function keeps the values from both sides.


Let us see the 'intersection_update( )' Function next. As the name suggests, the 'intersection_update( )' Function takes the values from both the sets and only value that is common in both Sets are taken.


Since, 'Kriti' is the common value in both the Sets, 'intersection_update( )' Function would just take the name 'Kriti'.


Example :


x = {"Mohan", "Kriti", "Salim"}
y = {"Sia", "Andrew", "Kriti"}
x.intersection_update(y)
print(x) 


Output :



  {'Kriti'}

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


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

java_Collections

Then what we have done is, put the names, 'Sia', 'Andrew' and 'Kriti' in another set 'y'.


y = {"Sia", "Andrew", "Kriti"}

java_Collections

And used the 'intersection_update( )' Function next.


x.intersection_update(y)

And since, 'Kriti' is present in both the Sets, only 'Kriti' will be present in the Set 'x'


java_Collections

And we get the below output,


{'Kriti'}

Next, let us see the 'symmetric_difference_update( )' Function. As the name suggests, the 'symmetric_difference_update( )' Function takes those values that are not present in both the sets.


Since, 'Kriti' is the common value in both the Sets, 'symmetric_difference_update( )' Function would exclude the name 'Kriti'.


Example :


x = {"Mohan", "Kriti", "Salim"}
y = {"Sia", "Andrew", "Kriti"}
x.symmetric_difference_update(y)
print(x) 


Output :



  {'Andrew', 'Sia', 'Salim', 'Mohan'}

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


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

java_Collections

Then what we have done is, put the names, 'Sia', 'Andrew' and 'Kriti' in another set 'y'.


y = {"Sia", "Andrew", "Kriti"}

java_Collections

And used the 'symmetric_difference_update( )' Function next.


x.symmetric_difference_update(y)

And since, 'Kriti' is present in both the Sets, only 'Kriti' would be excluded in the Set 'x'.Rest of the values would be taken.


java_Collections

And we get the below output,


{'Andrew', 'Sia', 'Salim', 'Mohan'}