Python - Set Methods
The Python has a number of methods and functions that are available for use with sets. These methods and functions are listed below in alphabetical order. For more details about any one of these, please visit its page.
Set Methods & Functions
Methods | Description |
---|---|
add() | Adds a specified element to the set |
clear() | Deletes all the elements from the set |
copy() | Returns a copy of the set |
difference() | Returns a set containing all elements of the given set which are not present in the specified sets or iterables |
difference_update() | Deletes all elements from the given set which are present in the specified sets or iterables |
discard() | Remove the specified element from the given set |
intersection() | Returns a set which contains common elements of two or more sets |
intersection_update() | Deletes all elements from the given set which are not present in all specified set(s) |
isdisjoint() | Returns True when two sets have any common element, else returns False |
issubset() | Returns True when all elements of the given set are present in specified set, else returns False |
issuperset() | Returns True when all elements of the specified set are present in the given set, else returns False |
pop() | Deletes a random element from the given set |
remove() | Removes the specified element from the given set |
symmetric_difference() | Returns a set with the symmetric differences of two sets |
symmetric_difference_update() | inserts the symmetric differences from the given set with another |
union() | Return a set containing the union of sets |
update() | Update the set with the union of this set and others |
Functions | Description |
len() | Returns total number of elements in the set |
set() | set function/constructor is used to create set from iterable like list, tuple, set, string, dictionary and range() etc. |