Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

set.intersection

Creates a set of members common to the supplied inputs.

Version: Python 3.14; repository image, production runtime not attested

Syntax

s.intersection(*others)

Creates a set of members common to the supplied inputs.

Duplicate values in the other iterable do not create duplicate results.

Only 3 occurs both in {2, 3} and in [3, 3, 8]. Two sightings of 3 yield one member of the new set. With several iterables, each result member must occur in all of them. The original still contains 2 and 3.

assert {1, 2, 3}.intersection([2, 2, 3], (2, 4)) == {2}

Examples

s = {2, 3}
print(sorted(s.intersection([3, 3, 8])))
print(sorted(s))

Result

[3]
[2, 3]

Only 3 occurs both in {2, 3} and in [3, 3, 8]. Two sightings of 3 yield one member of the new set. With several iterables, each result member must occur in all of them. The original still contains 2 and 3.

Sources