Python Set intersection() Method with Examples

Prerequisite:

Python set() Function with Examples

Set intersection() Method in Python:

The intersection() method returns a set containing the similarity(common items) of two or more sets.

If the comparison is done with more than two sets, 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.

For Example:

If P and Q are two distinct sets. A set intersection between P and Q is :

Let P={4,5,6,7}

Q={5,6,8,9}

P intersection Q = {5,6}

Since {5,6} are only the common elements in both the sets

Syntax:

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

Parameters

set1: This is Required. The set to look for similar items in

set2: This is Optional. The other set to look for similar items in. You are free to compare as many sets as you want. Use a comma to separate the sets.

Return Value:

The intersection() method returns the intersection of set A and all the other sets (passed as argument).

If the argument is not passed to an intersection(), a shallow copy of the set is returned (A).

Examples:

Example1:

Input:

Given first set =  {10, 20, 30, 40, 50}
Given second set = {100, 20, 80, 70, 30}
Given third set = {200, 40, 80, 30}

Output:

firstset intersection secondset =  {20, 30}
firstset intersection thirdset =  {40, 30}
secondset intersection thirdset =  {80, 30}

Example2:

Input:

Given first set =  {'a', 'b', 'c'}
Given second set = {'p', 'q', 'r'}
Given third set = {'q', 'a', 'k'}

Output:

firstset intersection secondset =  set()
firstset intersection thirdset =  {'a'}
secondset intersection thirdset =  {'q'}

Set intersection() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the first set as static input and store it in a variable.
  • Give the second set as static input and store it in another variable.
  • Give the third set as static input and store it in another variable.
  • Get the intersection(common elements) of the first set and second set using the intersection () method and print it.
  • Get the intersection(common elements) of the first set and third set using the intersection () method and print it.
  • Get the intersection(common elements) of the second set and third set using the intersection () method and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as static input and store it in a variable.
fst_set = {10, 20, 30, 40, 50}
# Give the second set as static input and store it in another variable.
scnd_set = {100, 20, 80, 70, 30}
# Give the third set as static input and store it in another variable.
thrd_set = {200, 40, 80, 30}
# Get the intersection(common elements) of the first set and second set using
# the intersection () method and print it.
print("firstset intersection secondset = ", fst_set.intersection(scnd_set))
# Get the intersection(common elements) of the first set and third set using
# the intersection () method and print it.
print("firstset intersection thirdset = ", fst_set.intersection(thrd_set))
# Get the intersection(common elements) of the second set and third set using
# the intersection () method and print it.
print("secondset intersection thirdset = ", scnd_set.intersection(thrd_set))

Output:

firstset intersection secondset =  {20, 30}
firstset intersection thirdset =  {40, 30}
secondset intersection thirdset =  {80, 30}

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the first set as user input using set(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the second set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the third set as user input using set(),map(),input(),and split() functions.
  • Store it in another variable.
  • Get the intersection(common elements) of the first set and second set using the intersection () method and print it.
  • Get the intersection(common elements) of the first set and third set using the intersection () method and print it.
  • Get the intersection(common elements) of the second set and third set using the intersection () method and print it.
  • The Exit of the Program.

Below is the implementation:

# Give the first set as user input using set(),map(),input(),and split() functions.
# Store it in a variable.
fst_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the second set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
scnd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
# Give the third set as user input using set(),map(),input(),and split() functions.
# Store it in another variable.
thrd_set = set(map(int, input(
   'Enter some random Set Elements separated by spaces = ').split()))
   
# Get the intersection(common elements) of the first set and second set using
# the intersection () method and print it.
print("firstset intersection secondset = ", fst_set.intersection(scnd_set))
# Get the intersection(common elements) of the first set and third set using
# the intersection () method and print it.
print("firstset intersection thirdset = ", fst_set.intersection(thrd_set))
# Get the intersection(common elements) of the second set and third set using
# the intersection () method and print it.
print("secondset intersection thirdset = ", scnd_set.intersection(thrd_set))

Output:

Enter some random Set Elements separated by spaces = 1 2 3
Enter some random Set Elements separated by spaces = 1 5 7
Enter some random Set Elements separated by spaces = 9 8 0
firstset intersection secondset = {1}
firstset intersection thirdset = set()
secondset intersection thirdset = set()