Python set intersection() method explained [Easy Examples]

 

Introduction to Python set intersection() method

In Python, Set is an unordered collection of a data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. The advantage of using the python set is that Python provides different mathematical operations which help us to analyze and process the sets. One of those operations is the python set intersection() method. In this tutorial as will learn about the python set intersection() method, its syntax, definition and will solve some examples. We will also cover how we can provide multiple arguments to operate the python set intersection() method and consider the time complexity of the method as well. Moreover, we will also discuss the difference between python set intersection() and union as well by taking examples.

 

Getting started with python set intersection() method

Before going into the python set intersection() method, let us first discuss a python set. A Python set has the following three main characteristics.

Advertisement
  • Sets are unordered.
  • Set elements are unique. Duplicate elements are not allowed.
  • A set itself may be modified, but the elements contained in the set must be of an immutable type.

These are the characteristics that make a set unique. See the following example of the python set.

a = [1,2,3,4,5]

Also, the set has some useful methods which are used to find and calculate different mathematical calculations. One of those methods is the python set intersection() method which we will discuss in this section.

 

Syntax of python set intersection() method

From mathematics, we know that the intersection of sets consists of all the elements that are present in all of the given sets. In python, we have a python set intersection() method that consists of all the common elements of the input sets. The simple syntax looks like this:

set.intersection(Python_sets)

Python set intersection() method can take one or more sets as an argument.  The set intersection() has many useful applications. For example, we can use set intersections to find the common favorites of two friends on a social networking application or to search for common skills of two or more employees on an HR application.

 

The return value of Python set intersection()

The return value of the python set intersection() is a new set consisting of the elements that are members of all sets, including the set it is called on. It has at most the number of elements as any other set involved in the intersection. It element cannot exceed any of the elements of given sets.  See the example below:

# creating sets
set1 = {1, 2, 3, 4}
set2 = {2, 3, 4, 5, 6}

# printing the python set intersection()
print(set1.intersection(set2))

Output:

Advertisement
{2, 3, 4}

Notice that the return type is also set. We can confirm it by apply the python type function to check the return data type. See the example below:

# creating sets
set1 = {1, 2, 3, 4}
set2 = {2, 3, 4, 5, 6}

# printing data type
print(type(set1.intersection(set2)))

Output:

<class 'set'>

Notice that the return type of python set intersection() is also a set. It actually returns a set containing all the common elements.

 

Examples of Python intersection() of two sets

Now let us take python sets examples and print out the intersection of the two sets using the python set intersection() method. See the example below:

# creating sets
set1 = {7, 2, 3, 4}
set2 = {5, 3, 3, 5, 6}

# printing
print("the intersection of two sets is:")
print(set1.intersection(set2))

Output:

the intersection of two sets is:
{3}

It is not necessary to apply the python set intersection() methods on a set of numbers, we can also apply the python set intersection() method on a set containing any kind of data type. See the example which prints the intersection of two sets containing string data type elements.

# creating sets
set1 = {"a", "b", "c", "d"}
set2 = {"b", "c", "f"}

# printing
print("the intersection two sets is:")
print(set1.intersection(set2))

Output:

the intersection two sets is:
{'c', 'b'}

In a similar way, we can apply the python set intersection() method on a set containing different data types elements. See the example below:

Advertisement
# creating sets
set1 = {1, "b", 2.9, "d"}
set2 = {1,"b", "c", "f", 2.9}

# printing
print("the intersection two sets is:")
print(set1.intersection(set2))

Output:

the intersection two sets is:
{1, 2.9, 'b'}

Notice that the output also contains different data type elements.

 

Examples of Python intersection() of two lists

One of the common differences between a python set and a python list is that list can contain duplicate elements in an ordered form while a list cannot contain duplicate elements and order is not important.

We can apply the python set intersection() method on lists to find out the common elements, but we cannot directly apply the intersection method as the python list does not have any intersection method. First, we have to apply the python set method on the list to convert it into the set and then apply the python set intersection() method. If we directly apply the intersection method, we will get an error.

See the example below:

# creating lists
list1 = [1,2, 3, 4, 5]
list2 =[3, 2, 4, 3, 6]

# printing
print("the intersection two lists is:")
print(list2.intersection(list1))

Output:

Python set intersection() method explained [Easy Examples]

Notice that we get an AttributeError because list objects have no attribute of intersection. Now let us solve this problem by applying the set method. See the example below:

# creating lists
list1 = [1,2, 3, 4, 5]
list2 =[3, 2, 4, 3, 6]

# printing 
print("the intersection two lists is:")
print(set(list2).intersection(set(list1)))

Output:

Advertisement
the intersection two lists is:
{2, 3, 4}

Notice the return type is again set, not list. If we want a return type to be a list, then we have to apply the list method to convert it back to the list. See the example below:

# creating lists
list1 = [1,2, 3, 4, 5]
list2 =[3, 2, 4, 3, 6]

# printing
print("the intersection two lists is:")
print(list(set(list2).intersection(set(list1))))

Output:

the intersection two lists is:
[2, 3, 4]

Notice that this time we get a list as the returned value.

 

Examples of Python union() and intersection() method

Python set union is the set of all the elements that exist in both sets, excluding the duplicates. It returns a set equal to or greater than the largest given set. See the example of the set union in the following example.

# creating sets
set1 = {1,2, 3, 4, 5}
set2 ={3, 2, 4, 3, 6} 

 # printing python set union
print("the union of two lists is:")
print(set2.intersection(set1))

Output:

the union of two lists is:
{1, 2, 3, 4, 5, 6}

We can also apply the python set intersection() and python set union method together. See the example below:

# creating sets
set1 = {1,2, 3, 4, 5}
set2 ={3, 2, 4, 3, 6}
set3 = {2, 3, 4, 5, 6, 7, 8}

# printing python set union and python set intersection()
print("the union and intersection of three sets is:")
print((set2.union(set1)).intersection(set3))

Output:

the union of two lists is:
{1, 2, 3, 4, 5, 6}

In a similar way, we can apply as many intersection and union operations on different sets as we want.

 

Python set intersection() with multiple arguments

So far we have learned how we can find the intersection of two python sets. In the same way, we can apply the python set intersection() method on more than two set by giving multiple arguments to the intersection method. The simple syntax looks like this:

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

Now let us take an example and see how the python set intersection() with multiple arguments works. See the example below:

# creating sets
set1 = {1,2, 3, 4, 5}
set2 ={3, 2, 4, 3, 6}
set3 = {2, 3, 4, 5, 6, 7, 8}
set4 = {2, 4, 6, 8}

# printing 
print("the intersection of multiple sets is:")
print(set2.intersection(set3, set1, set4))

Output:

the intersection of multiple sets is:
{2, 4}

In a similar way, we can apply the python set intersection() method on as many sets as we want by providing multiple arguments.

 

Python set intersection() using & symbol

In Python, there is another way to return the intersection of sets instead of using the python set intersection() method.  A much more concise way to write the set intersection() is the overloaded operator &. When we apply this symbol on two sets, it will return the intersection of the two sets. The simple syntax looks like this:

set1 & set2

Now let us take a real example and see how this method works. See the example below:

# creating sets
set1 = {1,2, 3, 4, 5}
set2 ={3, 2, 4, 3, 6}

# printing python set intersection() using & symbol
print("the intersection of two sets is:")
print(set1 & set2)<code>

Output:

the intersection of two sets is:
{2, 3, 4}

We can apply the same method on multiple sets as well. See the following example of multiple sets intersection.

# creating sets
set1 = {1,2, 3, 4, 5}
set2 ={3, 2, 4, 3, 6}
set3 = {2, 4, 6, 8, 0}
set4 = {1, 2, 3, 7, 8, 9}

# printing
print("the intersection of two sets is:")
print(set1 & set2 & set3 & set4)

Output:

the intersection of two sets is:
{2}

 

Summary

The python sets are immutable and cannot contain duplicate elements. Python allows us to perform different set operations including set union and set. Python set intersection() returns a set of all the common elements from the given sets.

In this tutorial, we learned about the python set intersection() method. We covered the syntax, take different examples, and also learned about multiple arguments as well. Moreover, we also discussed the difference between set union and set and solve examples using both operations at the same time.

 

Further Reading

Python set intersection
Python sets
Python set union

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment