![]() |
VOOZH | about |
The set.intersection() method in Python returns a new set containing only the elements that are common to all given sets. It compares two or more sets and keeps only shared values. The original sets remain unchanged.
Example: In this example, common elements between two sets are found using intersection().
{2, 3}
Explanation: a.intersection(b) returns elements present in both sets, only 2 and 3 are common.
set1.intersection(set2, set3, ...)
Example 1: In this example, the intersection of two and three sets is calculated.
{4, 6}
{4, 6}
Explanation:
Example 2: Here, the & operator is used as a shortcut for intersection.
{4, 6}
set()
set()
Explanation:
Example 3: In this example, intersection() is compared with symmetric_difference().
{4, 6}
{2, 5, 7, 8}
Explanation: