Easy Tutorial
❮ Ref Math Comb Ref Math Asin ❯

Python Set intersection() Method

Python Set


Description

The intersection() method is used to return the elements that are common to two or more sets, i.e., the intersection.

Syntax

The syntax for the intersection() method is:

set.intersection(set1, set2, ... etc)

Parameters

Return Value

Returns a new set.

Example

Returns a new set with elements that are in both set x and set y:

Example 1

x = {"apple", "banana", "cherry"}
y = {"google", "tutorialpro", "apple"}

z = x.intersection(y)

print(z)

Output:

{'apple'}

Calculates the intersection of multiple sets:

Example 2

x = {"a", "b", "c"}
y = {"c", "d", "e"}
z = {"f", "g", "c"}

result = x.intersection(y, z)

print(result)

Output:

{'c'}

Python Set

❮ Ref Math Comb Ref Math Asin ❯