Get The Most Affordable Hosting in the World!
Starting at just $1.87/month, Vercaa offers unbeatable pricing for world-class web hosting services.
Fast, reliable, and secure hosting to power your website without breaking the bank. Plus, enjoy a free CDN for faster loading times worldwide!
Get Started Now!Python's set class provides different methods to remove one or more items from a set object.
remove() Method
The remove() method removes the given item from the set collection, if it is present in it. However, if it is not present, it raises KeyError.
Syntax
set.remove(obj)
Parameters
-
obj − an immutable object
Example
It will produce the following output −
Set before removing: {'C', 'C++', 'Python', 'Java'} Set after removing: {'C', 'C++', 'Python'} lang1.remove("PHP") KeyError: 'PHP'
discard() Method
The discard() method in set class is similar to remove() method. The only difference is, it doesn't raise error even if the object to be removed is not already present in the set collection.
Syntax
Parameters
-
obj − An immutable object
Example
It will produce the following output −
Set before discarding C++: {'Java', 'C++', 'Python', 'C'} Set after discarding C++: {'Java', 'Python', 'C'} Set before discarding PHP: {'Java', 'Python', 'C'} Set after discarding PHP: {'Java', 'Python', 'C'}
pop() Method
The pop() method in set class removes an arbitrary item from the set collection. The removed item is returned by the method. Popping from an empty set results in KeyError.
Syntax
obj = set.pop()
Return value
The pop() method returns the object removed from set.
Example
It will produce the following output −
Set before popping: {'C++', 'C'} object popped: C++ Set after popping: {'C'} Traceback (most recent call last): obj = lang1.pop() ^^^^^^^^^^^ KeyError: 'pop from an empty set'
At the time of call to pop() for third time, the set is empty, hence KeyError is raised.
clear() Method
The clear() method in set class removes all the items in a set object, leaving an empty set.
Syntax
Example
It will produce the following output −
{'Java', 'C++', 'Python', 'C'} After clear() method set()
difference_update() Method
The difference_update() method in set class updates the set by removing items that are common between itself and another set given as argument.
Syntax
Parameters
-
obj − a set object
Example
It will produce the following output −
s1 before running difference_update: {1, 2, 3, 4, 5} s1 after running difference_update: {1, 2, 3} set()
difference() Method
The difference() method is similar to difference_update() method, except that it returns a new set object that contains the difference of the two existing sets.
Syntax
Parameters
-
obj − a set object
Return value
The difference() method returns a new set with items remaining after removing those in obj.
Example
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8} s3 = s1-s2: {1, 2, 3}
intersection_update() Method
As a result of intersection_update() method, the set object retains only those items which are common in itself and other set object given as argument.
Syntax
Parameters
-
obj − a set object
Return value
The intersection_update() method removes uncommon items and keeps only those items which are common to itself and obj.
Example
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8} s1 after intersection: {4, 5}
intersection() Method
The intersection() method in set class is similar to its intersection_update() method, except that it returns a new set object that consists of items common to existing sets.
Syntax
Parameters
-
obj − a set object
Return value
The intersection() method returns a set object, retaining only those items common in itself and obj.
Example
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8} s3 = s1 & s2: {4, 5}
symmetric_difference_update() method
The symmetric difference between two sets is the collection of all the uncommon items, rejecting the common elements. The symmetric_difference_update() method updates a set with symmetric difference between itself and the set given as argument.
Syntax
Parameters
-
obj − a set object
Example
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8} s1 after running symmetric difference {1, 2, 3, 6, 7, 8}
symmetric_difference() Method
The symmetric_difference() method in set class is similar to symmetric_difference_update() method, except that it returns a new set object that holds all the items from two sets minus the common items.
Syntax
Parameters
-
obj − a set object
Return value
The symmetric_difference() method returns a new set that contains only those items not common between the two set objects.
Example
It will produce the following output −
s1: {1, 2, 3, 4, 5} s2: {4, 5, 6, 7, 8} s1 = s1^s2 {1, 2, 3, 6, 7, 8}
The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.