Intersection() function Python (original) (raw)

Last Updated : 18 Apr, 2026

The set.intersection() method in Python returns a new set containing only the elements that are common to all given sets. It compares two or more sets and keeps only shared values. The original sets remain unchanged.

420851442

Intersection of two sets

**Example: In this example, common elements between two sets are found using intersection().

Python `

a = {1, 2, 3} b = {2, 3} print(a.intersection(b))

`

**Explanation: a.intersection(b) returns elements present in both sets, only 2 and 3 are common.

Syntax

set1.intersection(set2, set3, ...)

Examples

**Example 1: In this example, the intersection of two and three sets is calculated.

Python `

a = {2, 4, 5, 6} b = {4, 6, 7, 8} c = {4, 6, 8}

print(a.intersection(b)) print(a.intersection(b, c))

`

**Explanation:

**Example 2: Here, the & operator is used as a shortcut for intersection.

Python `

a = {2, 4, 5, 6} b = {4, 6, 7, 8} c = {1, 0, 12}

print(a & b) print(a & c) print(a & b & c)

`

**Explanation:

**Example 3: In this example, intersection() is compared with symmetric_difference().

Python `

a = {2, 4, 5, 6} b = {4, 6, 7, 8} print(a.intersection(b)) print(a.symmetric_difference(b))

`

Output

{4, 6} {2, 5, 7, 8}

**Explanation: