In this tutorial, I am going to discuss about all the built-in methods for sets in python. We have seen all the methods available for sets with short description but in this tutorial we are going to discuss about each built-in method for sets in detail.
Built-in Methods for Sets
As we know, python provides special built-in methods for set objects. So let’s see each of these methods and use.
add( )
The add()
method takes a python object as parameter andd appends that object to end of the set. The add()
method takes an immutable python object and appends it to the set. It takes only one immutable python object and adds that to the set.
Syntax of add()
method is :
set.add(value)
parameter : The add()
method takes an immutable python object as a paramaster. It takes only one parameter and appends that as an element to the end.
return : The add()
method does not return anything but it modifies the existing set by adding the parameter as an element to the end of the set.
Example :
set1 = {1,2,3,4}
print("Original set : ",set1)
set1.add(5)
print("After adding : ",set1)
set1.add((2.4,86))
print("Again after adding : ",set1)
Original set : {1, 2, 3, 4}
After adding : {1, 2, 3, 4, 5}
Again after adding : {1, 2, 3, 4, 5, (2.4, 86)}
clear( )
The clear()
method removes all the elements from the dictionary and makes it an empty set.
Syntax of clear()
method is :
set.clear( )
parameter : The clear()
method does not take any paramater.
return : The clear()
method does not return anything but it removes all the elements from the set and makes it an empty set.
Example :
print("Original set : ",set1)
set1.clear()
print("After clear() : ",set1)
Original set : {1, 2, 3, 4, 5, (2.4, 86)}
After clear() : set()
copy( )
The copy()
method is used to get a true copy of a set. It copies the original set into a new set and if we update any of the set then it will not affect other set.
Syntax of copy()
method is :
Set1 = set.copy( )
parameter : The copy()
method does not take any parameter.
return : The copy()
method returns a true copy of the set which means that both the sets are independent.
Example :
Set1 = { 1,2,3,4,5}
Set2 = Set1.copy()
print("Set1 = ",Set1)
print("Set2 = ",Set2)
Set1.add(6)
print("After updating Set1 ")
print("Set1 = ",Set1)
print("Set2 = ",Set2)
Set1 = {1, 2, 3, 4, 5}
Set2 = {1, 2, 3, 4, 5}
After updating Set1
Set1 = {1, 2, 3, 4, 5, 6}
Set2 = {1, 2, 3, 4, 5}
difference( )
The difference()
method is used to perform the set difference operation between two or more sets. It takes zero or more sets as parameter and returns the elements which are present only in the first set which calls the difference method and the elements which are also present in other sets in parameter are removed. If we do not give any paramater then the set is kept as it is.
Syntax of difference()
method is :
Set1 = set.difference( set2, set3, … )
parameter : The difference()
method takes either zero or more sets and performs set difference operation from the first set that calls the method.
return : The difference()
method returns a modified set which contains the the elements which are in the set which calls the method but not in others.
Example :
set1 = {1,2,3}
set2 = {3,4,5}
set3 = {1,7}
print("set1 = ",set1)
print("set2 = ",set2)
print("set3 = ",set3)
print("Difference between set1 and set2 : ",set1.difference(set2))
print("Difference between set1, set2 and set3 : ", set1.difference(set2, set3))
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {1, 7}
Difference between set1 and set2 : {1, 2}
Difference between set1, set2 and set3 : {2}
difference_update( )
The difference_update()
method is used to perform the set difference operation between two or more sets and then the set which calls the method is updated with the result. It takes zero or more sets as parameter and removes all elements from the calling set which are also present in the sets given in paramaters. If we do not give any paramater then the set is kept as it is.
Syntax of difference_update()
method is :
set.difference_update( set2, set3, … )
parameter : The difference_update()
method takes either zero or more sets and removes the elements from calling set which are also present in the sets gievn in parameters.
return : The difference_update()
method does not return anything but it modifies the calling set by removing common elements which are present in the sets in parameter.
Example :
set1 = {1,2,3}
set2 = {3,4,5}
print("set1 = ",set1)
print("set2 = ",set2)
set1.difference_update(set2)
print("set1 after performing difference update with set2: ",set1)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1 after performing difference update with set2: {1, 2}
discard( )
The discard()
method is used remove an element from the set. As we know set is mutable but it takes only immutable python objects as elements. The discard()
method takes one parameter which is an element of a set. If the set contains that element then it updates the set by removing that element and if the set does not have that element then it keeps the set as it is.
Syntax of discard()
method is :
set.discard(value)
parameter : The discard()
method takes one value as parameter which should be an immutable python object. It checks if the value is present in the set then it removes the element and updates the set, otherwise it keeps the set as it is.
return : The discard()
method does not return anything but it modifies the set by removing the parameter value from the set if ti present in the set as an element.
Example :
set4 = {'a', 'b', 'c', 'd', 'e'}
print("Original set : ", set4)
set4.discard('b')
print("After discard('b') : ",set4)
set4.discard('h')
print("After discard('h') : ",set4)
Original set : {'e', 'a', 'd', 'b', 'c'}
After discard('b') : {'e', 'a', 'd', 'c'}
After discard('h') : {'e', 'a', 'd', 'c'}
intersection( )
The intersection()
method is used to perform the set intersection operation between two or more sets. It takes zero or more sets as parameter and keeps elements which are present in all the sets. If we do not give any paramater then the set is kept as it is.
Syntax of intersection()
method is :
Set1 = set.intersection( set2, set3, … )
parameter : The intersection()
method takes either zero or more sets and performs set intersection operation among all the sets.
return : The intersection()
method returns a modified set which contains all the elements which are present in all the sets.
Example :
set1 = {1,2,5}
set2 = {3,4,5,1}
set3 = {1,7,5,3}
print("set1 = ",set1)
print("set2 = ",set2)
print("set3 = ",set3)
print("Intersection between set1 and set2 : ",set1.intersection(set2))
print("Intersection between set1, set2 and set3 : ", set1.intersection(set2, set3))
set1 = {1, 2, 5}
set2 = {1, 3, 4, 5}
set3 = {1, 3, 5, 7}
Intersection between set1 and set2 : {1, 5}
Intersection between set1, set2 and set3 : {1, 5}
intersection_update( )
The intersection_update()
method is used to perform the set intersection operation between two or more sets and then the set which calls the method is updated with the result. It takes zero or more sets as parameter and keeps all elements present in all the sets. If we do not give any paramater then the set is kept as it is.
Syntax of intersection_update()
method is :
set.intersection_update( set2, set3, … )
parameter : The intersection_update()
method takes either zero or more sets and performs set intersection operation for all the sets then it updates the set which is calling method with the result.
return : The intersection_update()
method does not return anything but it modifies the calling set by updating the calling set with the elements which are present in all the sets.
Example :
set1 = {3,4,5,1}
set2 = {1,7,5,3}
print("set1 = ",set1)
print("set2 = ",set2)
set1.intersection_update(set2)
print("set1 after performing intersection update with set2: ",set1)
set1 = {1, 3, 4, 5}
set2 = {1, 3, 5, 7}
set1 after performing intersection update with set2: {1, 3, 5}
isdisjoint( )
The isdisjoint()
method is used to check if two sets have a null intersection, means both the sets have null intersection. This method returns True if both sets have no common elements, and returns False if both the sets have atleast one common element.
Syntax of isdisjoint()
method is :
set.isdisjoint(set1)
parameter : The isdisjoint()
method takes a set as a paramater to check whether the two sets have any comon elements.
return : The isdisjoint()
method returns True if the two sets does not have any common element, otherwise it returns False.
Example :
A = {1,2,3}
B = {4,5,6}
C = {3,4,7}
print("Set A : ",A)
print("Set B : ",B)
print("Set C : ",C)
print("A is disjoint from B = ", A.isdisjoint(B))
print("A is disjoint from C = ", A.isdisjoint(C))
print("B is disjoint from C = ", B.isdisjoint(C))
Set A : {1, 2, 3}
Set B : {4, 5, 6}
Set C : {3, 4, 7}
A is disjoint from B = True
A is disjoint from C = False
B is disjoint from C = False
issubset( )
The issubset()
method is used to check if a set is subset of other set. This method takes a set as a paramater and checks if the set which calls the method is a subset of this set. A is a subset of B means that B contains all the lements of A but the reverse may not be true.
Syntax of issubset()
method is :
set1.issubset(set2)
The above code returns True if set1 is a subset of set2 then the method, otherwise it returns False.
parameter : The issubset()
method takes a set as a paramater to check whether the set calling the method is a subset of the set in the parameter.
return : The issubset()
method returns True if the set is subset of the set from parameter, otherwise it returns False.
Example :
A = {1,2,4}
B = {1,2}
C = {3,5}
print("Set A : ",A)
print("Set B : ",B)
print("Set C : ",C)
print("B is a subset of set A ? : ",B.issubset(A))
print("C is a subset of set A ? : ",C.issubset(A))
Set A : {1, 2, 4}
Set B : {1, 2}
Set C : {3, 5}
B is a subset of set A ? : True
C is a subset of set A ? : False
issuperset( )
The issuperset()
method is used to check if a set is issuperset of other set. This method takes a set as a paramater and checks if the set which calls the method is a issuperset of this set. A is a issuperset of B means that A contains all the elements of B.
Syntax of issuperset()
method is :
set1.issuperset(set2)
The above code returns True if set1 is a issuperset of set2, otherwise it returns False.
parameter : The issuperset()
method takes a set as a paramater to check whether the set calling the method is a issuperset of the set in the parameter.
return : The issuperset()
method returns True if the set is issuperset of the set from parameter, otherwise it returns False.
Example :
P = {'a','b','c','d','e'}
Q = {'a','b','c'}
R = {'d','e','f'}
print("Set P : ",P)
print("Set Q : ",Q)
print("Set R : ",R)
print("P is superset of Q ? : ",P.issuperset(Q))
print("P is superset of R ? : ",P.issuperset(R))
Set P : {'a', 'e', 'c', 'd', 'b'}
Set Q : {'a', 'b', 'c'}
Set R : {'d', 'e', 'f'}
P is superset of Q ? : True
P is superset of R ? : False
pop( )
The pop()
method removes and returns an arbitrary element from the set. It removes the first element from the set by default. If the set is empty then pop() method raises KeyError.
Syntax of pop()
method is :
set.pop( )
parameter : The pop()
method does not have any paramater.
return : The pop()
method returns the value which is removed from the set and modifies the existing set by removing an arbitrary element from the set and if the set is empty then it throws KeyError.
Example :
X = {1,2,3}
print("Original set : ",X)
print("Element : ",X.pop(), ", Set after pop() : ",X)
print("Element : ",X.pop(), ", Set after pop() : ",X)
print("Element : ",X.pop(), ", Set after pop() : ",X)
print("Element : ",X.pop(), ", Set after pop() : ",X)
Original set : {1, 2, 3}
Element : 1 , Set after pop() : {2, 3}
Element : 2 , Set after pop() : {3}
Element : 3 , Set after pop() : set()
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
in
4 print("Element : ",X.pop(), ", Set after pop() : ",X)
5 print("Element : ",X.pop(), ", Set after pop() : ",X)
----> 6 print("Element : ",X.pop(), ", Set after pop() : ",X)
KeyError: 'pop from an empty set'
remove( )
The remove()
method removes the given element from the set if it exists in the set. It takes one immutable python object as a parameter because a set contains only immutable python objects as its elements. If the set contains that value then the element is removed from the set otherwise, it will raise KeyError.
Syntax of remove()
method is :
set.remove(value)
parameter : The remove()
method takes one immutable python object as parameter. If the value is present in the set then it updates the set by removing the element from the set, otherwise it will raise KeyError.
return : The remove()
method does not return anything but if the value from the paramater is present in the set then it modifies the set by removing that value, otherwise the method will raise KeyError.
Example :
Y = {1,2,3,4,5}
print("Original set : ", Y)
Y.remove(2)
print("After removing 2 : ",Y)
Y.remove(5)
print("After removing 5 : ",Y)
Y.remove(7)
Original set : {1, 2, 3, 4, 5}
After removing 2 : {1, 3, 4, 5}
After removing 5 : {1, 3, 4}
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
in
5 Y.remove(5)
6 print("After removing 5 : ",Y)
----> 7 Y.remove(7)
KeyError: 7
symmetric_difference
The symmetric_difference()
method is used to get symmetric difference between two sets. Symmetric difference between two sets means a set of all elements which are either in one set or another but not in both. It contains all unique elements from both the sets and no ccommon elements between two sets. This method takes only one set as a parameter and returns a new set which contains the symmetric difference between the two sets.
Syntax of symmetric_difference()
method is :
set.symmetric_difference(set1)
parameter : The symmetric_difference()
method takes one set as a parameter and performs the symmteric difference operation between two sets.
return : The symmetric_difference()
method returns a modified set which contains the result of symmetric difference operation betweeen the two sets.
Example :
A = {1,2,3}
B = {3,4,5}
C = {5,6,7}
print("Set A : ", A )
print("Set B : ", B )
print("Set C : ", C )
print("Symmetric difference between set A and B : ",A.symmetric_difference(B))
print("Symmetric difference between set B and C : ",B.symmetric_difference(C))
print("Symmetric difference between set A and C : ",A.symmetric_difference(C))
Set A : {1, 2, 3}
Set B : {3, 4, 5}
Set C : {5, 6, 7}
Symmetric difference between set A and B : {1, 2, 4, 5}
Symmetric difference between set B and C : {3, 4, 6, 7}
Symmetric difference between set A and C : {1, 2, 3, 5, 6, 7}
symmetric_difference_update
The symmetric_difference_update()
method is used to get symmetric difference between two sets. Symmetric difference between two sets contains all unique elements from both the sets and no common elements between two sets. This method takes only one set as a parameter and updates the original set with the symmetric difference between the two sets.
Syntax of symmetric_difference_update()
method is :
set.symmetric_difference_update(set1)
parameter : The symmetric_difference_update()
method takes one set as a parameter and performs the symmteric difference operation between two sets.
return : The symmetric_difference_update()
does not return anything but it modifies the original set with the result of symmetric difference between the two sets.
Example :
set1 = {1,9,4,6,8}
set2 = {2,4,7,0}
print("set1 = ",set1)
print("set2 = ",set2)
set1.symmetric_difference_update(set2)
print("set1 after performing symmetric difference update with set2: ",set1)
set1 = {1, 4, 6, 8, 9}
set2 = {0, 2, 4, 7}
set1 after performing symmetric difference update with set2: {0, 1, 2, 6, 7, 8, 9}
union( )
The union()
method is used to perform the set union operation between two or more sets. It takes zero or more sets as parameter and keeps elements from all the sets. If we do not give any paramater then the set is kept as it is.
Syntax of union()
method is :
Set1 = set.union(set2, set3, … )
parameter : The union()
method takes either zero or more sets and performs set union operation among all the sets.
return : The union()
method returns a modified set which contains all the elements from all the sets.
Example :
set1 = {1,2,3}
set2 = {2,3,4,5}
set3 = {'a','b','c'}
print("set1 = ",set1)
print("set2 = ",set2)
print("set3 = ",set3)
print("Union between set1 and set2 : ",set1.union(set2))
print("Union between set1, set2 and set3 : ", set1.union(set2, set3))
set1 = {1, 2, 3}
set2 = {2, 3, 4, 5}
set3 = {'a', 'b', 'c'}
Union between set1 and set2 : {1, 2, 3, 4, 5}
Union between set1, set2 and set3 : {1, 2, 3, 4, 5, 'c', 'a', 'b'}
update( )
The update()
method is used to perform the set union operation between two or more sets and then the set which calls the method is updated with the result. It takes zero or more sets as parameter and keeps all elements from all the sets. If we do not give any paramater then the set is kept as it is.
Syntax of update()
method is :
set.update(set2, set3, … )
parameter : The update()
method takes either zero or more sets and performs set union operation for all the sets then it updates the set which is calling method with the result.
return : The update()
method does not return anything but it modifies the calling set by updating the calling set with the elements from all the sets.
Example :
set1 = {1,2,3}
set2 = {'a','b','c'}
print("set1 = ",set1)
print("set2 = ",set2)
set1.update(set2)
print("set1 after performing intersection update with set2: ",set1)
set1 = {1, 2, 3}
set2 = {'a', 'b', 'c'}
set1 after performing intersection update with set2: {1, 2, 3, 'c', 'a', 'b'}
Let’s conclude this tutorial here. In this tutorial we have seen all built-in methods provided by python for set objects with examples.
If you have any questions please comment below, also share your views and suggestions in the comment box.