# Python – Intersection of two or more Sets

In this tutorial, we will look at how to get the intersection of two or more sets in Python with the help of some examples.

The intersection operation between two sets results in a set containing all the common elements between both the sets. Let’s look at an example.

Here, you can see that the result of intersection of sets A and B is a set containing just three which is only the common element between both the sets.

Python comes with a built-in set data structure to implement a set. It also has a number of additional functions to help you with common operations on sets such as union, intersection, difference, etc.

To get the intersection between two or more sets in Python, you can use the set `intersection()` function. The following is the syntax:

```# intersection of two sets - a and b
a.intersection(b)```

It returns a new set containing the common elements between sets a and b. Let’s see an example.

```# create two sets
a = {1,2,3}
b = {3,7,2,6}
# intersection between two sets
a.intersection(b)```

Output:

`{2, 3}`

We get the resulting set as {2, 3} as these are the only elements common between the intersecting sets.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

To get the intersection of more than two sets at once, you can use the following syntax.

```# intersection of more than two sets, for example, a,b,c,d
a.intersection(b,c,d)```

Pass the sets (apart from the first one) as arguments to the `intersection()` function. It returns a set containing the common elements from all the sets involved in the intersection operation. Let’s look at an example.

```# create four sets
a = {1,2,3}
b = {4,5,2,1}
c = {0,1,7,2}
d = {4,3,2}
# intersection between the above four sets
a.intersection(b,c,d)```

Output:

`{2}`

Two is the only common element between the four sets hence we get {2} as the result of the intersection operation.

Alternatively, you can also use the `&` operator to get the intersection between two or more sets. For example –

```# alternative method
e = a & b & c & d
print(e)```

Output:

`{2}`

We get the same result as above.

Note that the `&` operator for intersection only works on sets where as you can pass other iterables like lists, tuples, etc. to the set `intersection()` function.

You might also be interested in –