Python Set intersection() MethodPython Functions


The intersection() method returns a new set which consists of the common elements in both sets.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

A = {1, 2, 3, 4}
B = {1, 2 ,4,5,6,7,9}
C = {1, 2,11,3}

print("The intersection between set A and set B is",A.intersection(B))

print("The intersection between set A and set C is",A.intersection(C))

print("The intersection between set C and set B is",B.intersection(C))

Output:

The intersection between set A and set B is {1, 2, 4}
The intersection between set A and set C is {1, 2, 3}
The intersection between set C and set B is {1, 2}

Syntax

x.intersection(a, b,... etc)

Parameters

The parameter is the set to search for equal items in. Optional parameters are other sets to search for equal items in.

Return Value

It returns the intersection of two sets.

© 2021 Programtips.org, all rights reserved. Privacy Policy | Contact Us