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.
x = {"Mohan", "Kriti", "Salim"} x.add("Nikhil") print(x)
So, in the above code we have created a 'Set' and initialised to the variable 'x'.
Next, we have used the 'add( )' Function to add 'Nikhil' to the Set.
And 'Nikhil' gets inserted at any location. And that's decided by Python.
And we get the below output,
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.
x = {"Mohan", "Kriti", "Salim"} x.add("Kriti") print(x)
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.
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:
Let us see the 'update( )' Function first.
x = {"Mohan", "Kriti", "Salim"} y = {"Sia", "Andrew", "Kriti"} x.update(y) print(x)
So, in the above code we have created a 'Set' and initialised to the variable 'x'.
Then what we have done is, put the names, 'Sia', 'Andrew' and 'Kriti' in another set 'y'.
And used the 'update( )' Function to add the new Set 'y'(With values 'Sia', 'Andrew' and 'Kriti') to the existing Set 'x'.
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.
And we get the below output,
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'.
x = {"Mohan", "Kriti", "Salim"} y = {"Sia", "Andrew", "Kriti"} x.intersection_update(y) print(x)
So, in the above code we have created a 'Set' and initialised to the variable 'x'.
Then what we have done is, put the names, 'Sia', 'Andrew' and 'Kriti' in another set 'y'.
And used the 'intersection_update( )' Function next.
And since, 'Kriti' is present in both the Sets, only 'Kriti' will be present in the Set 'x'
And we get the below output,
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'.
x = {"Mohan", "Kriti", "Salim"} y = {"Sia", "Andrew", "Kriti"} x.symmetric_difference_update(y) print(x)
So, in the above code we have created a 'Set' and initialised to the variable 'x'.
Then what we have done is, put the names, 'Sia', 'Andrew' and 'Kriti' in another set 'y'.
And used the 'symmetric_difference_update( )' Function next.
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.
And we get the below output,