Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python Set intersection() Method

Python Set intersection() Method

Python3 Lists Python Sets


Description

The intersection() method returns elements contained in two or more sets, that is, the intersection.

Syntax

Syntax of the intersection() method:


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

Parameters

Return Value

Returns a new set.

Example

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

Example 1

x

= {

"

apple

"

,

"

banana

"

,

"

cherry

"

}

y

= {

"

google

"

,

"

runoob

"

,

"

apple

"

}

z

=

x

.

intersection

(

y

)

print

(

z

)

The output is:


{'apple'}

Compute the intersection of multiple sets:

Example 1

x

= {

"

a

"

,

"

b

"

,

"

c

"

}

y

= {

"

c

"

,

"

d

"

,

"

e

"

}

z

= {

"

f

"

,

"

g

"

,

"

c

"

}

result

=

x

.

intersection

(

y

,

z

)

print

(

result

)

The output is:


{'c'}

Python3 Lists Python Sets