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

# Perform intersection of two sets
A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}
print(A.intersection(B))
# Prints {'red'}
Set intersection

Equivalent Operator &

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

A = {'red', 'green', 'blue'}
B = {'yellow', 'red', 'orange'}
# by method
print(A.intersection(B))
# Prints {'red'}
# by operator
print(A & B)
# Prints {'red'}

Intersection of Multiple Sets

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

A = {'red', 'green', 'blue'}
B = {'yellow', 'orange', 'red'}
C = {'blue', 'red', 'black'}
# by method
print(A.intersection(B,C))
# Prints {'red'}
# by operator
print(A & B & C)
# Prints {'red'}