Python Set Intersection Methods with Best Examples

Set in Python is one of the fundamental data structures that store sequential elements.

It is represented by curly brackets and separates every element with a comma.

Sets are mutable and unordered, which means that indexing in a set is not accurate.

What is Python set intersection?

The set intersection in Python can be described as the production of a new set composed of the elements shared by both sets.

In simple words, it returns a set that contains elements that are common to all the sets.

Additional information from the Python official documentation, set intersection() returns a new set with elements common to the set and all others.

Example:

Set1 = {1, 2, 3, 4, 5}
Set2 = {4, 5, 6, 7, 8, 9, 10}

# compute intersection between Set1 and Set2
print(Set1.intersection(Set2))

Illustration of the above example:

Output:

{4, 5}

Syntax of set intersection()

Below is the syntax:

set1.intersection(set2, set3, set4...)

Parameters of set intersection()

Set intersection() accepts an arbitrary number of arguments.

Return value from set intersection()

Set intersection() returns a new set that contains the common to all the given sets.

There are two methods to perform an intersection in a set:

  • The intersection() method
  • The & method.

Python set intersection() method:

Using the set intersection() operator in Python.

Example 1: Working with two sets intersection()

The intersection of two sets using string elements.

Set1 = {"P", "R", "I", "N", "C", "E"}
Set2 = {"C", "E", "S", "A", "R"}

print(Set1.intersection(Set2))

Illustration of the above example program:

Output:

{'E', 'C', 'R'}

Example 2: Working with three sets intersection()

The calculation of the intersection of three sets.

Set1 = {1, 2, 3, 4, 5, 6}
Set2 = {2, 4, 6, 8, 10, 12}
Set3 = {3, 6, 9, 12, 15, 18}

# intersection between set 1 and set 2
print(Set1.intersection(Set2))

# intersection between set 2 and set 3
print(Set2.intersection(Set3))

# intersection between set 3 and set 1
print(Set3.intersection(Set1))

# intersection of all the sets
print("Intersection of All sets:",Set1.intersection(Set2,Set3))

Illustration of the above example program:

Output:

Set1 and Set2: {2, 4, 6}
Set2 and Set3: {12, 6}
Set3 and Set1: {3, 6}
Intersection of All sets: {6}

Example 3: Working with four sets intersection()

The calculation of the intersection of four sets.

Set1 = {1, 2, 3, 4, 5, 6}
Set2 = {2, 4, 6, 8, 10, 12}
Set3 = {3, 6, 9, 12, 15, 18}
Set4 = {4, 8, 12, 16, 20, 24}

print(Set1.intersection(Set4))
print(Set2.intersection(Set4))
print(Set3.intersection(Set4))
print(Set1.intersection(Set2, Set3, Set4))

Illustration of the above example program:

Output:

{4}
{8, 4, 12}
{12}
set()

Python set intersection using & operator method

You can also find the intersection of sets using & operator.

Example:

Set1 = {1, 2, 3, 4, 5, 6}
Set2 = {2, 4, 6, 8, 10, 12}
Set3 = {3, 6, 9, 12, 15, 18}
Set4 = {4, 8, 12, 16, 20, 24}

print(Set1 & Set3)
print(Set1 & Set4)
print(Set1 & Set2 & Set3)
print(Set1 & Set2 & Set3 & Set4)

Illustration of the above example program:

Output:

{3, 6}
{4}
{6}
set()

How do you calculate the intersection between two sets in Python?

To calculate the intersection between the two sets, we have to use the len() method.

Example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}

result = len(set(set1 & set2))

      
print("The number of common elements in both sets:",result)

Output:

The number of common elements in both sets: 1

Python set intersection complexity

Tested the time complexity of the set intersection, according to them.

The runtime complexity of the set.intersection() method on a set with n elements and a set argument with m elements is O(min(n, m)) because you need to check for the smaller set whether each of its elements is a member of the larger set.

Checking membership is O(1), so the runtime complexity is O(min(n, m)) * O(1) = O(min(n, m)).

Summary

After reading this article, we should now have a good understanding of how to use the intersection() method and set intersection & operator method.

You are now ready to use it in an actual program.

If you found the information in this article useful, please consider sharing it with others who may find it useful as well.

Leave a Comment