Set Methods In Python
Now, we are going to learn and explore some set-related methods, with help of which, we can do a bunch of different things, like adding some elements to the set, clearing the set, creating a copy of the set, and much much more. Let’s have a look at these simple methods.
First of all, here is a table, which has many different set methods, with some descriptions and examples, so that you can have an idea.
Set Methods In Python
For the below table, in the example, the set that we would use is going to be as follows –
myset = {“GyaniPandit”, “Python”, 2.5, 6}
Method | Description | Example |
add() | using this method, we add new elements into the set | myset={“GyaniPandit”,”Python”, 2.5, 6} myset.add(31) print(myset) output → {2.5, 6, ‘GyaniPandit’, ‘Python’, 31} |
clear() | using this method, we can clear the set. | myset={“GyaniPandit”,”Python”, 2.5, 6} myset.clear() print(myset) output → set() |
copy() | this creates a copy of the set | myset={“GyaniPandit”,”Python”, 2.5, 6} newset = myset.copy() print(newset) output → {‘Python’, 2.5, 6, ‘GyaniPandit’} |
difference() | this gives a set containing the difference between two or more sets. In the example, we are calculating the difference between set1 and set2, which means that the newset will contain the values in set1 but not in set2. | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set2 = {“Python”, 2,3} newset = set1.difference(set2) print(newset) output → {‘GyaniPandit’, 2.5, 6} |
intersection() | returns a set which is intersection of the sets | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set2 = {“Python”, 2,3} newset = set1.intersection(set2) print(newset) output → {‘Python’} |
union() | this returns a set which is the union of the sets | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set2 = {“Python”, 2,3} newset = set1.union(set2) print(newset) output → {‘GyaniPandit’, 2.5, 2, 3, 6, ‘Python’} |
remove() | it removes the specified element | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set1.remove(2.5) print(set1) output → {‘Python’, ‘GyaniPandit’, 6} |
isdisjoint() | checks whether two sets are disjoint (sets that have no elements in common) | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set2 = {“Python”, 2,3} value = set1.isdisjoint(set2) print(value) output → False |
issubset() | This method checks whether one set is the subset of another. | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set2 = {“Python”,2.5} value = set2.issubset(set1) print(value) output → True |
discard() | With this method, some specified element is removed from the set. If the element is not there in the set, then we do not get an error. | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set1.discard(6) print(set1) output → {2.5, ‘Python’, ‘GyaniPandit’} |
difference_update() | removes elements from a set if present in both (this does an operation on the original set while the difference method returns a new set) | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set2 = {“Python”, 2,3} set1.difference_update(set2) print(set1) output → {2.5, ‘GyaniPandit’, 6} |
pop() | removes an element from a set. The element removed is random. It is returned by the method. | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set1.pop() print(set1) output → {‘GyaniPandit’, 6, ‘Python’} |
issuperset() | This method is used to check whether or not one set is the superset of the other set. | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set2 = {“Python”,2.5} value = set1.issuperset(set2) print(value) output → True |
update() | This method updates the set, by adding values from the other set (or other iterable) | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set2 = {8,9,3} set1.update(set2) print(set1) output → {2.5, 3, 6, 8, ‘Python’, ‘GyaniPandit’, 9} |
symmetric_difference() | This method returns a set that contains elements from both sets discarding the elements which are present in both sets. | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set2 = {“Python”,2.5} newset=set1.symmetric_difference(set2) print(new set) output → {‘GyaniPandit’, 6} |
symmetric_difference_update() | This method works similarly to the method symmetric_difference(), just with the difference that it does not return a new set, instead, it makes changes in the original set. | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set2 = {“Python”,2.5} set1.symmetric_difference_update(set2) print(set1) output → {‘GyaniPandit’, 6} |
intersection_update() | This method works similar to the method intersection(), with a difference that it does not return a new set. All the changes are made on the original set instead. | set1 = {“GyaniPandit”, “Python”, 2.5, 6} set2 = {“Python”, 2,3} set1.intersection_update(set2) print(set1) output → {‘Python’} |
Now, Let’s get into the details of these methods one by one, where we would focus on the implementation of these methods, and understand how we can use them.