Intersection() function Python Last Updated : 09 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Python set intersection() method returns a new set with an element that is common to all set The intersection of two given sets is the largest set, which contains all the elements that are common to both sets. The intersection of two given sets A and B is a set which consists of all the elements which are common to both A and B. Python Set intersection() Method Syntax: Syntax: set1.intersection(set2, set3, set4....) Parameters: any number of sets can be passed Return: Returns a set which has the intersection of all sets(set1, set2, set3...) with set1. It returns a copy of set1 only if no parameter is passed. Python Set intersection() Method Example: Python3 s1 = {1, 2, 3} s2 = {2, 3} print(s1.intersection(s2)) Output: {2, 3}Example 1: Working of set intersection() Python3 # Python3 program for intersection() function set1 = {2, 4, 5, 6} set2 = {4, 6, 7, 8} set3 = {4, 6, 8} # intersection of two sets print("set1 intersection set2 : ", set1.intersection(set2)) # intersection of three sets print("set1 intersection set2 intersection set3 :", set1.intersection(set2, set3)) Output: set1 intersection set2 : {4, 6} set1 intersection set2 intersection set3 : {4, 6}Example 2: Python set intersection operator(&) We can also get intersections using '&' operator. Python3 # Python3 program for intersection() function set1 = {2, 4, 5, 6} set2 = {4, 6, 7, 8} set3 = {1, 0, 12} print(set1 & set2) print(set1 & set3) print(set1 & set2 & set3) Output: {4, 6} set() set()Example 3: Python set intersection opposite symmetric_difference() is an opposite to the Python Set intersection() method. Python3 # Python3 program for intersection() function set1 = {2, 4, 5, 6} set2 = {4, 6, 7, 8} set3 = {1, 0, 12} print(set1.symmetric_difference(set2)) print(set1.symmetric_difference(set3)) print(set2.symmetric_difference(set3)) Output: {2, 5, 7, 8} {0, 1, 2, 4, 5, 6, 12} {0, 1, 4, 6, 7, 8, 12}Example 4: Python set intersection empty Intersection of empty sets returns an empty set. Python3 set1 = {} set2 = {} # union of two sets print("set1 intersection set2 : ", set(set1).intersection(set(set2))) Output: set1 intersection set2 : set() Comment More infoAdvertise with us Next Article Intersection() function Python S Striver Follow Improve Article Tags : Misc Python python-set Python-Built-in-functions Practice Tags : Miscpythonpython-set Similar Reads Python Set Methods A Set in Python is a collection of unique elements which are unordered and mutable. Python provides various functions to work with Set. In this article, we will see a list of all the functions provided by Python to deal with Sets. Adding and Removing elementsWe can add and remove elements form the s 2 min read Set add() Method in Python The set.add() method in Python adds a new element to a set while ensuring uniqueness. It prevents duplicates automatically and only allows immutable types like numbers, strings, or tuples. If the element already exists, the set remains unchanged, while mutable types like lists or dictionaries cannot 4 min read Python Set clear() Method Python Set clear() method removes all elements from the set. Python Set clear() Method Syntax: Syntax: set.clear() parameters: The clear() method doesn't take any parameters.  Return: None Time complexity : The time complexity of set.clear() function on a set with n element is O(n) . Example 1: Pyth 2 min read set copy() in python The copy() method returns a shallow copy of the set in python. If we use "=" to copy a set to another set, when we modify in the copied set, the changes are also reflected in the original set. So we have to create a shallow copy of the set such that when we modify something in the copied set, change 2 min read Python Set discard() Function Python discard() is a built-in method to remove elements from the set. The discard() method takes exactly one argument. This method does not return any value. Example: In this example, we are removing the integer 3 from the set with discard() in Python. Python3 my_set = {1, 2, 3, 4, 5} my_set.discar 3 min read Python Set | difference_update() The difference_update() method helps in an in-place way of differentiating the set. The previously discussed set difference() helps to find out the difference between two sets and returns a new set with the difference value, but the difference_update() updates the existing caller set.If A and B are 1 min read Python Set difference() In Python, the difference() method is used to find elements that exist in one set but not in another. It returns a new set containing elements from the first set that are not present in the second set. This operation is similar to the subtraction of sets (A - B), where only unique elements from the 2 min read issuperset() in Python Python Set issuperset() method returns True if all elements of a set B are in set A. Then Set A is the superset of set B. Python issuperset() Method Syntax: Syntax: A.issuperset(B) Parameter: Any other Set to compare with Return: boolean value  Python issuperset() exampleExample 1: Working of issubs 1 min read Python Set issubset() Method Python set issubset() method returns True if all elements of a set A are present in another set B which is passed as an argument, and returns False if all elements are not present in Python. Python Set issubset() Method SyntaxSyntax: set_obj.issubset(other_set) Parameter: other_set: any other set to 2 min read Python Set isdisjoint() Method Python set isdisjoint() function check whether the two sets are disjoint or not, if it is disjoint then it returns True otherwise it will return False. Two sets are said to be disjoint when their intersection is null. Python set isdisjoint() Method Syntax: Syntax: set1.isdisjoint(set2) Parameters: 2 min read Like