Python Set - intersection method

The intersection method of set class creates an intersection set of two sets by selecting the elements that are commonly present in both the sets.

Set Updated 15 Sep 2016 1 min read

Method signature

intersection(anotherSet_in)

Method overview

  • The intersection of two sets is given by this method which selects the elements that are common to both the sets.

Parameters

anotherSet_in - The second set from which common elements are drawn along with the current/this set, to form the intersection set

Example

python
# Example Python program that creates an intersection of two sets

# Set 1

            airCraftParts   = {"Wheels","Engine","Gear","Wings","Tail"}

# Set 2

            carParts        = {"Wheels","Engine","Gear"}

# Set 1 ∩ Set 2

            automobileParts = airCraftParts.intersection(carParts)

            print("Intersection of two sets:")

            print(automobileParts)

Output

Output
Intersection of two sets:

{'Gear', 'Engine', 'Wheels'}