Python Set intersection() Method

Returns a set with items common to all the specified sets

Usage

The intersection() method returns a new set of items that are common to all the specified sets.

You can specify as many sets as you want, just separate each set with a comma.

If you want to modify the original set instead of returning a new one, use intersection_update() method.

Syntax

set.intersection(set1,set2…)

Python set intersection() method parameters
ParameterConditionDescription
set1, set2…OptionalA comma-separated list of one or more sets to search for common items in

Basic Example

Example: Perform intersection of two sets

A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}

print(A.intersection(B))

Output:

{'red'}

Venn Diagram:

Set intersection

Equivalent Operator &

Set intersection can be performed with the & operator as well.

Example:

A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}

# by method
print(A.intersection(B))

# by operator
print(A & B)

Output:

{'red'}

Intersection of Multiple Sets

Multiple sets can be specified with either the operator or the method.

Example:

A = {'red', 'green', 'blue'}
B = {'yellow', 'orange', 'red'}
C = {'blue', 'red', 'black'}

# by method
print(A.intersection(B,C))

# by operator
print(A & B & C)

Output:

{'red'}