Built-in Set Functions in Python
The following table lists all the functions that can be used with the set type in Python 3.
Method | Description |
---|---|
set.add() | Adds an element to the set. If an element is already exist in the set, then it does not add that element. |
set.clear() | Removes all the elements from the set. |
set.copy() | Returns a shallow copy of the set. |
set.difference() | Returns the new set with the unique elements that are not in the another set passed as a parameter. |
set.difference_update() | Updates the set on which the method is called with the elements that are common in another set passed as an argument. |
set.discard() | Removes a specific element from the set. |
set.intersection() | Returns a new set with the elements that are common in the given sets. |
set.intersection_update() | Updates the set on which the instersection_update() method is called, with common elements among the specified sets. |
set.isdisjoint() | Returns true if the given sets have no common elements. Sets are disjoint if and only if their intersection is the empty set. |
set.issubset() | Returns true if the set (on which the issubset() is called) contains every element of the other set passed as an argument. |
set.pop() | Removes and returns a random element from the set. |
set.remove() | Removes the specified element from the set. If the specified element not found, raise an error. |
set.symmetric_difference() | Returns a new set with the distinct elements found in both the sets. |
set.symmetric_difference_update() | Updates the set on which the instersection_update() method called, with the elements that are common among the specified sets. |
set.union() | Returns a new set with distinct elements from all the given sets. |
set.update() | Updates the set by adding distinct elements from the passed one or more iterables. |