Python Built-in Sets methods

Python Built-in set methods

Python contains the following methods to be used with the sets.

SN Method Description
1 add(item) It adds an item to the set. It has no effect if the item is already present in the set.
2 clear() It deletes all the items from the set.
3 copy() It returns a shallow copy of the set.
4 difference_update(…) It modifies this set by removing all the items that are also present in the specified sets.
5 discard(item) It removes the specified item from the set.
6 intersection() It returns a new set that contains only the common elements of both the sets. (all the sets if more than two are specified).
7 intersection_update(…) It removes the items from the original set that are not present in both the sets (all the sets if more than one are specified).
8 Isdisjoint(…) Return True if two sets have a null intersection.
9 Issubset(…) Report whether another set contains this set.
10 Issuperset(…) Report whether this set contains another set.
11 pop() Remove and return an arbitrary set element that is the last element of the set. Raises KeyError if the set is empty.
12 remove(item) Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
13 symmetric_difference(…) Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
14 symmetric_difference_update(…) Update a set with the symmetric difference of itself and another.
15 union(…) Return the union of sets as a new set.
(i.e. all elements that are in either set.)
16 update() Update a set with the union of itself and others