Set Methods in Python

Set Methods in Python

Set methods in Python are used to manipulate sets and find information that relates to it. There are a number of methods of set class and functions that act on a set object. Some of those methods and functions are mentioned below in the form of a table.

 

Methods and Functions Description
add() It is a method and is used to add the specified element in the set object.
clear() It is a method and is used to delete all the elements of the set object. It makes the set empty.
copy() It is a method and returns copy of the set object that calls it.
difference() It is a method and returns all elements of the given set that are not present in the specified sets or iterables.
difference_update() It is a method and is used to delete all the elements from the given set that are present in the specified sets or iterables.
discard() It is a method and is used to discard or delete the specified element from the set object.
intersection() It is a method and returns elements that are common in two or more specified set objects.
intersection_update() It is a method and is used to delete all the elements from the given set if they are not present in all the specified sets or iterables.
isdisjoint() It is a method and returns True if two sets have any common element, if not, returns False.
issubset() It is a method and returns True if all the elements of the given set are present in the specified set, if not, returns False.
issuperset() It is a method and returns True if all the elements of the specified set are present in the given set, if not, returns False.
pop() It is a method and is used to delete a random element from the given set object.
remove() It is a method and is used to delete the specified set from the given set object.
symmetric_difference() It is a method and returns a set with symmetric differences of two sets.
symmetric_difference_update() It is a method and is used to insert the symmetric differences from the given set with another.
union() It is a method and returns set containing the union of sets.
update() It is a method and updates the set with the union of this set and others.
len() It is a function and is used to return number of elements the specified set object consists of.
set() It is a function/constructor to create a set object with iterables like list, tuple, string, set, dictionary, etc.

 

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *