The **Python Set intersection() method** allows two sets to be compared and evaluated as to whether they intersect. This method lets you check whether a set contains a specific element.

In this post, we will be discussing the Python Set intersection() method. When working with sets, it is important to remember that they are unordered and do not have duplicates. So let us get started!

Table of Contents

## What is Set Intersection( ) Method ?

The set intersection() method in Python returns a collection of unique items in the first or second set. This can be useful for finding duplicate items, calculating intersections between sets, and more.

### Working of Set Intersection( ) Method

## Set Intersection( ) Method Using set.intersection

The set.intersection method in Python returns a collection of unique items in either the first or second set. This can be useful for finding duplicate items, calculating intersections between sets, and more.

```
setA = {1, 2, 3}
setB = {2, 7}
print(setA.intersection(setB))
```

#### Output

## Set Intersection( ) Method Using ‘&’ Operator

The & operator sets the intersection of two arrays or two collections. This can be useful when combining elements from both arrays into a single array or collection.

```
setA = {2, 3, 5, 1}
setB = {3, 1, 7, 8}
setC = {1, 0, 12}
print(setA & setB)
print(setA & setC)
print(setA & setB & setC)
```

#### Output

## Set Intersection( ) Method Using Empty Set

What if the sets are empty and do not include any elements? Set intersection returns the common elements of two sets. Since there are no common elements in the supplied sets, the result will be an empty set after performing the intersection.

```
setA = {}
setB = {}
print(set(setA).intersection(set(setB)))
```

#### Output

## Set Intersection( ) Method vs Set Intersection Operator

The set intersection() function accepts any iterable, such as lists, strings, and dictionaries. Whereas a set data structure may find the intersection with the set intersection operator (&), it will raise an error on any iterables conditions. Remember that the intersection method will convert every iterables to sets before intersecting them and producing the result.

### Set Intersection( ) Method

```
X = {1, 3, 5}
Y = [1, 5, 9]
result = X.intersection(Y)
print(result)
```

#### Output

### Set Intersection( ) Operator

```
setA = {1, 6, 5}
setB = [1, 5, 8]
set = setA & setB
print(set)
```

#### Output

## FAQs

### What if there is no intersection in a set?

If there is no intersection, that is, no common element in setA and setB, then the function will return an empty set.

### What is the time complexity of set intersection method in Python?

Because you have to check for the smaller set’s membership in the larger set, Python’s set intersection has a time complexity of O(min(n, m)) when applied to a set with n elements and a set argument with m elements.

Also visit the article, Python Set Union() Method to learn more about Python set methods.

## Conclusion

In this article, we covered **Python set intersection () method** in detail. Python has some of the best set operations and methods. This means that if you want to explore more about sets, you can easily use these statements in your scripts. For example, if there are duplicate records in the database, you can use the intersection() method to find them easily. You can also calculate the intersections between two sets by using the intersection() method. Thanks for reading.