Python Set intersection() Method

Python Set intersection() Method

  • The intersection() method returns a set that contains the similarity between two or more sets. 

  • Meaning: The returned set contains only items that exist in both sets, or in all sets if the comparison is done with more than two sets.

                       A = {1, 2, 3, 4}
                       B = {2, 3, 4,  9}
                       C = {2, 4, 9 10}

                       Then,
                       A∩B = B∩A ={2, 3, 4}
                       A∩C = C∩A ={2, 4}
                       B∩C = C∩B ={2, 4, 9}

                       A∩B∩C = {2, 4}

 

Syntax

  • set.intersection(set1, set2 ... etc)

Return value:

  • The intersection() function 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. 

 

 

Comments