Python Set Operations (Union, Intersection, Difference and Symmetric Difference)

Last Updated : 16 Jul, 2026

Sets are a fundamental data structure that store unique elements. Python provides built-in operations for performing set operations such as union, intersection, difference and symmetric difference. In this article, we understand these operations one by one.

Union

The union of two sets returns a new set containing all unique elements from both sets. Duplicate values are included only once.

Syntax:

set1 | set2
set1.union(set2)

Example: The example below demonstrates how to find the union of two sets using both the | operator and the union() method.

Python
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

r1 = a | b
print("Using |:", r1)

r2 = a.union(b)
print("Using union():", r2)

Output
Using |: {1, 2, 3, 4, 5, 6}
Using union(): {1, 2, 3, 4, 5, 6}

Explanation: Both the | operator and the union() method return a new set containing all unique elements from both sets. Since sets do not allow duplicate values, the common elements (3 and 4) appear only once in the result.

Intersection

The intersection of two sets returns a new set containing only the elements that are present in both sets.

Syntax:

set1 & set2
set1.intersection(set2)

Example: The example below demonstrates how to find the intersection of two sets using both the & operator and the intersection() method.

Python
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

r1 = a & b
print("Using &:", r1)

r2 = a.intersection(b)
print("Using intersection():", r2)

Output
Using &: {3, 4}
Using intersection(): {3, 4}

Explanation: Both the & operator and the intersection() method return a new set containing only the elements that are common to both sets. In this example, 3 and 4 are present in both sets, so they appear in the result.

Difference

The difference of two sets returns a new set containing the elements that are present in the first set but not in the second set.

Syntax:

set1 - set2
set1.difference(set2)

Example: The example below demonstrates how to find the difference between two sets using both the - operator and the difference() method.

Python
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

r1 = a - b
print("Using -:", r1)

r2 = a.difference(b)
print("Using difference():", r2)

Output
Using -: {1, 2}
Using difference(): {1, 2}

Explanation: Both the - operator and the difference() method return a new set containing the elements that are present in the first set but not in the second set. In this example, 1 and 2 exist only in the first set, so they appear in the result.

Symmetric Difference

The symmetric difference of two sets returns a new set containing the elements that are present in either of the two sets, but not in both.

Syntax:

set1 ^ set2
set1.symmetric_difference(set2)

Example: The example below demonstrates how to find the symmetric difference between two sets using both the ^ operator and the symmetric_difference() method.

Python
a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

r1 = a ^ b
print("Using ^:", r1)

r2 = a.symmetric_difference(b)
print("Using symmetric_difference():", r2)

Output
Using ^: {1, 2, 5, 6}
Using symmetric_difference(): {1, 2, 5, 6}

Explanation: Both the ^ operator and the symmetric_difference() method return a new set containing elements that are present in either set, but not in both. In this example, 3 and 4 are common to both sets, so they are excluded, while 1, 2, 5, and 6 appear in the result.

Comment