Python Set intersection() Method

Returns a new set with items that all sets include.

This is in contrast to intersection_update which modifies all sets involved, instead of simply returning a new set.

Syntax

Python
set.intersection(set1 [, set2, ...])

Parameters

ParameterDescription
set1 [, set2, ...] Required. One or more sets whose elements will be used to create the new set containing the elements comment to all sets.

Example

Python
desktopComponents = {'memory', 'processor', 'hard drive'}
laptopComponents = {'memory', 'processor', 'SSD', 'camera', 'keyboard'}
commonComponents = laptopComponents.intersection(desktopComponents)
print(f'Common components are: {commonComponents}')

Output

Common components are: {'processor', 'memory'}