Method | Description |
---|---|
Used to add an element to a set | |
Used to remove an element from a set. If the element is not present in the set, raise a KeyError | |
Used to remove an element from set if it is a member. (Do nothing if the element is not in set) | |
Used to removes and returns an arbitrary set element. Raise KeyError if the set is empty | |
Used to remove all elements form a set | |
Used to returns a shallow copy of a set | |
Used to get the union of sets in a new set | |
Used to update a set with the union of itself and others | |
Used to get the difference of two or more sets as a new set | |
Used to remove all elements of another set from this set | |
Used to returns the intersection of two sets as a new set | |
Updates the set with the intersection of itself and another | |
Returns True if two sets have a null intersection | |
Returns True if this set contains another set | |
Returns True if another set contains this set | |
Returns the symmetric difference of two sets as a new set | |
Updates a set with the symmetric difference of itself and another |
The set add() method adds a given element to a set if the element is not present in the set.
The add() method doesn't add an element to the set if it's already present in it otherwise it will get added to the set.
Example:
set_1 = {1 ,2 ,3 }
print (set_1)
set_1.add (4 )
print (set_1) ## add 4 to the set
set_1.add (2 ) ## duplicate entry
print (set_1)
The built-in method, remove() in Python, removes the element from the set only if the element is present in the set, just as the discard() method does but If the element is not present in the set, then an error or exception is raised.
If the element is present in the set:
Example:
set_1 = {1 ,2 ,3 }
print (set_1)
set_1.remove (2 )
print (set_1) ## remove 2 from the set
set_1.remove (2 ) ## Returns a Keyerror
print (set_1)
The built-in method, discard() in Python, removes the element from the set only if the element is present in the set.
If the element is not present in the set, then no error or exception is raised and the original set is printed.
Example:
set_1 = {1 ,2 ,3 }
print (set_1)
set_1.discard (2 )
print (set_1) ## remove 2 from the set
set_1.discard (2 ) ## Returns the same set
print (set_1)
Python set pop() Method removes any a random element from the set and returns the removed element.
Example:
set_1 = {1 ,2 ,3 }
print (set_1)
set_1.pop ()
print (set_1) ## remove any random element from the set
set_1.pop ()
print (set_1)
set_1.pop ()
print (set_1)
set_1.pop ()
print (set_1) ## returns Keyerror
Python Set clear() method removes all elements from the set.
Example:
set_1 = {1 ,2 ,3 }
print (set_1)
set_1.clear ()
print (set_1)
The copy() method returns a shallow copy of the set in python.
If we use “=” to copy a set to another set, when we modify in the copied set, the changes are also reflected in the original set.
So we have to create a shallow copy of the set such that when we modify something in the copied set, changes are not reflected back in the original set.
Example:
set_1 = {1 ,2 ,3 }
print (set_1)
set_2 = set_1.copy ()
print (set_2)
Union() Method returns a new set which contains all the items from the original set.
Union of two given sets is the set which contains all the elements of both the sets.
The union of two given sets A and B is a set which consists of all the elements of A and all the elements of B such that no element is repeated.
We can use '|' operator to find union of sets
Example:
set_1 = {1 ,2 ,3 ,4 }
set_2 = {2 ,4 ,8 ,16 }
print (set_1.union (set_2))
set_3 = {3 ,6 ,9 ,12 }
print (set_1.union (set_2,set_3))
print (set_1 | set_3)
print (set_1.union (set_2).union (set_3))
update() function in set adds elements from a set (passed as an argument) to the set.
update() method takes only a single argument. The single argument can be a set, list, tuples or a dictionary. It automatically converts into a set and adds to the set.
Example:
eleven = {1 ,40 ,11 ,5 }
vecna = [33 ,"hello" ,"mike" ,11 ]
eleven.update (vecna)
print (eleven)
eleven.update ({123 }) ## {} are required.
eleven.update ({"12345as" })
print (eleven)
The difference between the two sets in Python is equal to the difference between the number of elements in two sets.
The function difference() returns a set that is the difference between two sets.
Let’s try to find out what will be the difference between two sets A and B.
Then (set A – set B) will be the elements present in set A but not in B and (set B – set A) will be the elements present in set B but not in set A.
set difference can be obtained by "-" operator also.
Example:
a = {1 ,2 ,3 ,4 ,5 }
b = {1 ,2 ,4 ,6 ,8 }
print (a.difference (b)) ## a-b
## a-b equal to the elements present in a but not in b
print (b.difference (a))
## b-a equal to the elements present in b but not in a
print (a- b)
print (b- a)
The difference_update() method helps in an in-place way of differentiating the set.
The previously discussed set difference() helps to find out the difference between two sets and returns a new set with the difference value, but the difference_update() updates the existing caller set.
If A and B are two sets. The set difference() method will get the (A – B) and will return a new set. The set difference_update() method modifies the existing set.
If (A – B) is performed, then A gets modified into (A – B), and if (B – A) is performed, then B gets modified into (B – A).
The function returns None and changes the value of the existing set. In this example, we will get the difference between two sets and show how the difference_update works.
Example:
a = {1 ,2 ,3 ,4 ,5 }
b = {1 ,2 ,4 ,6 ,8 }
a.difference_update (b)
print (a)
#modifies a
intersection() method returns a new set with an element that is common to all set.
The intersection of two given sets is the largest set, which contains all the elements that are common to both sets.
The intersection of two given sets A and B is a set which consists of all the elements which are common to both A and B.
We can also get intersections using ‘&’ operator.
Example:
one = {1 ,2 ,3 ,4 }
two = {9 ,6 ,2 ,3 }
print (one.intersection (two))
print (two.intersection (one))
print (one & two)
The intersection_update() method removes the items that is not present in both sets (or in all sets if the comparison is done between more than two sets).
The intersection_update() method is different from the intersection() method, because the intersection() method returns a new set, without the unwanted items, and the intersection_update() method removes the unwanted items from the original set.
Example:
one = {1 ,2 ,3 ,4 }
two = {9 ,6 ,2 ,3 }
one.intersection_update (two)
print (one)
set isdisjoint() function check whether the two sets are disjoint or not, if it is disjoint then it returns True otherwise it will return False.
Two sets are said to be disjoint when their intersection is null.
Example:
set1 = {1 ,2 ,3 ,4 }
set2 = {5 ,6 ,7 ,9 }
set3 = {5 ,2 ,8 }
print (set1.isdisjoint (set2)) ## True
print (set1.isdisjoint (set3)) ## False
issuperset() method returns True if all elements of a set B are in set A. Then Set A is the superset of set B.
Example:
set1 = {1 ,4 ,3 ,2 }
set2 = {4 ,3 ,9 ,10 ,1 ,2 ,55 ,123 ,34 }
print (set1.issuperset (set2)) ## False
print (set2.issuperset (set1)) ## True
set issubset() method returns True if all elements of a set A are present in another set B which is passed as an argument, and returns False if all elements are not present in Python.
Example:
set1 = {1 ,4 ,3 ,2 }
set2 = {4 ,3 ,9 ,10 ,1 ,2 ,55 ,123 ,34 }
print (set1.issubset (set2)) ## True
print (set2.issubset (set1)) ## False
symmetric_difference() Method is used to get the elements present in either of the two sets, but not common to both the sets.
Let’s look at the Venn diagram of the symmetric_difference between two sets.
We can also find symmetric difference from two sets using ‘^’ operator in Python.
Example:
set1 = {1 ,4 ,3 ,2 }
set2 = {4 ,3 ,9 ,10 ,1 ,2 ,55 ,123 ,34 }
print (set1.symmetric_difference (set2))
The symmetric difference of two sets is the set of elements which are in either of the sets but not in both of them.
symmetric_difference() method returns a new set which contains symmetric difference of two sets.
The symmetric_difference_update() method updates the set calling symmetric_difference_update() with the symmetric difference of sets.
Example:
set1 = {1 ,4 ,3 ,2 }
set2 = {4 ,3 ,9 ,10 ,1 ,2 ,55 ,123 ,34 }
set1.symmetric_difference_update (set2)
print (set1)