Python intersection of sets

In this python tutorial, we will discuss the intersection of sets in python with a few examples and also we will cover these below topics:

  • What is the intersection?
  • Python intersection of sets
  • Python sets intersection example
  • Python intersection of two lists
  • Python count intersection of lists
  • Python intersection of multiple sets
  • Python intersection of multiple lists
  • Python intersection of three sets
  • Python count intersection of sets
  • Python set intersection using & operators
  • Python find the intersection of multiple sets

What is the intersection?

The intersection of two sets has only the elements common to both sets is called an intersection in Python. If an element is in just one set then it is not part of the intersection.

Syntax:

set.intersection(other_sets)

Python intersection of sets

Lets us see, how to perform intersection of sets in python.

The intersection method returns a new set with the elements that are common to both sets. In the below example “apple” is present in both sets so, it returns the new set.

Example:

m1 = {"Samsung","Apple","OnePlus"}
m2 = {"Oppo","Apple","Vivo"}
i = m1.intersection(m2)
print(i)

In this output, we can see that the python intersection of sets is performed. You can refer to the below screenshot.

Python intersection of sets
Python intersection of sets

You may like Python binary tree implementation and How to read video frames in Python.

Python sets intersection example

Lets us see, python sets intersection example in python.

In this example, we will compare 3 sets and return a set with items that are present in all 3 sets. We can see that “c” is present in all the 3 sets.

Example:

s1 = {"c", "y", "z"}
s2 = {"a", "c", "e"}
s3 = {"f", "g", "c"}
result = s1.intersection(s2, s3)
print(result)

In this output, we can see that python sets intersection examples. You can refer to the below screenshot.

Python sets intersection example
Python sets intersection example

Read: How to add dropdown navbar in Django

Python intersection of two lists

Now, we will see python intersection of two lists.

The intersection of two lists means we have to take all the elements which are common in both lists. In the below example “10 and 6” is present in both. so, it returns the new list.

Example:

def intersection(l1, l2): 
    return list(set(l1) & set(l2)) 
l1 = [10, 8, 6, 5, 2]
l2 = [6, 3, 10, 4, 1]
print(intersection(l1, l2)) 

In this output, we can see that the python intersection of two lists is performed. You can refer to the below screenshot.

Python intersection of two lists
Python intersection of two lists

Read: How to Add Elements in a Set in Python

Python count intersection of lists

Here, we will see python count intersection of lists.

To count the intersection of lists we will use “len(set(l1) & set(l2))”. Here, ” & “ is an intersection element common to both. It will return the count as “2” because “101 and 88” are common to both the lists.

Example:

l1 = [101, 120, 88, 16, 14]
l2 = [88, 108, 66, 101, 140]
r = len(set(l1) & set(l2)) 
print(r)

In this output, we can see that the python count intersection of lists is performed. You can refer to the below screenshot.

Python count intersection of lists
Python count intersection of lists

Python intersection of multiple sets

Lets us see, how to perform intersection of multiple sets in python.

The intersection of multiple sets contains all elements common to all the sets. In the below example “20 and 15” is present in all the sets. so, it returns the new sets.

Example:

s1 = {15, 18, 16, 20, 25}
s2 = {20, 14, 15, 12, 22}
s3 = {15, 12, 20, 23, 19}
i = set. intersection(s1,s2,s3)
print(i) 

In this output, we can see that the python intersection of multiple sets is performed. You can refer to the below screenshot.

Python intersection of multiple sets
Python intersection of multiple sets

Python intersection of multiple lists

Now, we will see python intersection of multiple lists.

  • The intersection of two Python lists contains multiple lists in different ways.
  • First, we have initialized the two lists with multiple lists.
  • Now, iterate the list using list comprehension or loop.

Example:

def intersection(l1, l2): 
    return [item for item in l1 if item in l2] 
l1 = [['Naina', 'Raj'], ['Sweety', 'Pooja']] 
l2 = [['Sweety', 'Pooja'], ['Sam', 'Sid']] 
print(intersection(l1, l2)) 

In this output, we can see that the python intersection of multiple lists is performed. You can refer to the below screenshot.

Python intersection of multiple lists
Python intersection of multiple lists

Python intersection of three sets

Lets us see, how to perform intersection of three sets in python.

The intersection of three sets contains all elements common to all the sets. In the below example “108” is present in all the sets. so, it returns the new sets.

Example:

set1 = {101, 105, 108}
set2 = {108, 130, 111}
set3 = {119, 125, 108}
i = set. intersection(set1,set2,set3)
print(i) 

In this output, we can see that the python intersection of three sets is performed. You can refer to the below screenshot.

Python intersection of three sets
Python intersection of three sets

Python count intersection of sets

To count the intersection of sets in Python, we will use “len(set(set1) & set(set2))”. Here, ” & “ is an intersection element common to both. It will return the count as “3” because “10, 8, and 6” are common to both the sets.

Example:

set1 = {10, 12, 8, 6, 4}
set2 = {8, 18, 6, 10, 5}
r = len(set(set1) & set(set2)) 
print(r)

In this output, we can see python count intersection of sets is performed. You can refer to the below screenshot.

Python count intersection of sets
Python count intersection of sets

Python set intersection using & operators

To find the intersection of set we can also use ” & ” operators. The & operator will return the common element present in sets.

Example:

set1 = {10, 12, 8, 6, 4}
set2 = {88, 18, 60, 101, 5}
set3 = {12, 22, 6, 20, 28}
print(set1 & set3)

In this output, we can see python set intersection using & operators are performed. You can refer to the below screenshot.

Python set intersection using & operators
Python set intersection using & operators

Python find the intersection of multiple sets

To find the intersection of multiple sets in Python, we have to take 4 sets and then we can use the “&” operator to get another set, which will have common elements present in all 4 sets.

Example:

set1 = {1,2,3,4,5}
set2 = {2,3,5,7,9}
set3 = {3,5,10,11,12}
set4 = {8,7,5,4,3}
result = set1 & set2 & set3 & set4
print(result)

In this output, we can see python find the intersection of multiple sets. You can refer to the below screenshot.

Python find the intersection of multiple sets
Python find the intersection of multiple sets

You may like the following Python tutorials:

In this tutorial, we have learned about Python intersection of sets, and also we have covered these topics:

  • What is the intersection?
  • Python intersection of sets
  • Python sets intersection example
  • Python intersection of two lists
  • Python count intersection of lists
  • Python intersection of multiple sets
  • Python intersection of multiple lists
  • Python intersection of three sets
  • Python count intersection of sets
  • Python set intersection using & operators
  • Python find the intersection of multiple sets