Python

Python set intersection

Many data type exists in Python to store multiple data. The set is one of them. The way to find out the common values within two or more sets is called intersection. Theoretically, the ‘∩’ symbol denotes the intersection between the sets. The intersection() function and the ‘&’ operator are used in Python to find out the common values of multiple sets. The uses of intersection() function and ‘&’ operator have shown in this tutorial by using multiple python scripts.

The intersection() function:

This function is used to create a new set by intersecting one or more sets. The syntax of the intersection() function has given below.

Syntax:

set1.intersection(*setn)

The setn argument of the function is optional, and the ‘*’ symbol indicates that one or more sets can be used as the argument value of this function. When no argument is passed into the function, then the shallow copy of set1 will return; otherwise, the common values of set1 and other sets defined as the argument values will return.

Examples of set intersection:

Example-1: Use of intersection () function

Create a python file with the following script to find out the common values of two or more sets by using the intersection() function. Three sets are declared in the script. The first intersection() function has been used to print the copy of the set, s1. The second intersection() function has been used to find and print the common values of the sets, s1 and s2. The third intersection() function has been used to find and print the common values of the sets, s1, s2, and s3.

# Define three sets

s1 = {44, 23, 12, 91}

s2 = {12, 45, 23, 78, 67}

s3 = {83, 12, 36, 20copt

# Intersect 1 set

print("Using intersection() for one set: ", s1.intersection())

# Intersect 2 sets

print("Using intersection() for two set: ", s1.intersection(s2))

# Intersect 2 sets

print("Using intersection() for three set :", s1.intersection(s2, s3))

Output:

The following output will appear after executing the above script. The number 12 is common in all the sets, and it has been printed in the third output. The numbers 12 and 23 are common in the sets s1 and s2. These numbers have been printed in the second output. The values of the set, s1, have been printed in the first output.

Example-2: Use of ‘&’ operator

Create a python file with the following script to find out the common values of two or more sets by using the ‘&’ operator. Four sets have been declared in the script. The first three print () functions will print common values of two sets. The last print () function will print the common values of four sets.

# Define four sets

s1 = {44, 23, 12, 91}

s2 = {12, 45, 23, 78, 67}

s3 = {83, 12, 36, 44}

s4 = {23, 78, 22}

# Intersect between two sets

print("Intersect values of s1 and s2: ", s1 & s2)

print("Intersect values of s1 and s3: ", s1 & s3)

print("Intersect values of s1 and s4: ", s1 & s4)

# Intersect 4 sets

print("Intersect values of s1, s2, s3, and s4 :", s1 & s2 & s3 & s4)

Output:

The following output will appear after executing the above script. The common values between s1 and s2 sets are 12 and 23, which are printed in the first output. The common values between s1 and s3 sets are 12 and 44, which are printed in the second output. The common value between s1 and s3 sets is 23, and it has been printed in the third output. There is no common value among the four sets. So, the empty set has been printed in the fourth output.

Example-3: Filter the set values using the intersection

Create a python file with the following script to find out the prime numbers from the set named ‘numbers’ by using another set of prime numbers and the intersection() function. The set named ‘primes’ contains 8 prime numbers. The intersection() function will find out prime numbers from the ‘numbers’ set by finding the common values between ‘numbers’ and ‘primes’ sets. The values of the ‘numbers’ set and the filtered values of the ‘numbers’ set will be printed as the output.

# Define a set of prime and non-prime numbers

numbers = {33, 7, 56, 9, 94, 17, 50, 19, 10, 11}

# Define a set of prime numbers

primes = {3, 5, 7, 11, 13, 17, 19, 23}

# Print the values of the numbers

print("The values of the set are:\n", numbers)

# Create a new set after filtering the prime numbers from the numbers set

primeValues = numbers.intersection(primes)

# Print the values of the prime numbers from the numbers set

print("The prime values of the set are:\n", primeValues)

Output:

The following output will appear after executing the above script. The ‘numbers’ set contains 10 values, and 4 of them are prime numbers. All values of the ‘numbers’ set have been printed in the first output, and the prime numbers from the ‘numbers’ set have been printed in the second output.

Example-4: Intersection of sets without argument

Create a python file with the following script to know how to create a set by using the intersection() function and set object. The values of the set will be printed later.

# Declare an object of set

s1 = set('Linux')

# Create a set by using intersection()

s2 = s1.intersection()

# Print the set values

print(" The values of the set are:\n", s2)

Output:

The following output will appear after executing the above script.

Conclusion:

Different ways to intersect the values of two or more python sets and the multiple uses of the intersection() function have been shown in this tutorial. The python users will be able to intersect two or more sets in different ways after properly practicing this tutorial’s examples.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.