A complete guide to mathematical set operations in Python

Mathematical Set Operations in Python
Python’s set
is an unordered collection in Python. It can be used to compute standard math operations, such as intersection, union, difference, and symmetric difference. Other
collections — like list, tuple, and dictionary — don’t support set operations. Dict
view objects are set-like, which allows set operations. Refer to my story on Python sets.
This article will explore the mathematical operations supported by set objects in detail.
Let’s Look at the Mathematical Operations Supported by the Set Object
union()
update()
intersection()
intersection_update()
difference()
difference_update()
symmetric_difference()
symmetric_difference_update()
isdisjoint()
issubset()
issuperset()
Set operations can be done in two ways. By using the method or by using an operator.
‘union()’
Return a new set with elements from the set and the other
. It’s performed by union()
or by using the |
operator
Syntax
union(*others)
set | other | ...

Example 1: Find the union of two sets — A
and B
It’ll return a new set containing elements from set A
and set B
. But it won’t repeat elements. All elements in the set are unique.
A={1,2,3,4,5} B={2,4,6,8} print (A.union(B))#Output:{1, 2, 3, 4, 5, 6, 8} print (A|B)#Output:{1, 2, 3, 4, 5, 6, 8}
Example 2: Find the union of more than two sets
A={1,2,3,4,5} B={2,4,6,8,10} C={1,3,5,7,9} print (A|B|C)#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} print (A.union(B,C))#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Difference between the union()
method and the |
operator:
union()
: It’ll accept any iterable as an argument|
operator: It’ll accept only a set as an argument. Otherwise, it’ll raise aTypeError
.
Example 3: Giving iterable
as an argument in the union()
method
A={1,2,3,4,5} #iterable is given as list print (A.union([2,4,6]))#Output:{1, 2, 3, 4, 5, 6} #iterable is given as tuple print (A.union((2,4,6)))#Output:{1, 2, 3, 4, 5, 6} #iterable is given as range object print (A.union(range(5,10)))#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9} #iterable is given as a dictionary print (A.union({'a':6,'b':7}))#Output:{1, 2, 3, 4, 5, 'b', 'a'}
Example 4: Giving iterable
as an argument for the|
operator
A={1,2,3,4,5} B=[1,2,3] print (A|B) #Output:TypeError: unsupported operand type(s) for |: 'set' and 'list'
‘update()’
It updates the set, adding elements from the other
. But it won’t repeat elements. All elements in the set are unique. It’s performed by using update()
or by using the |=
operator. The return type is None
. It’ll modify the original set itself.
Syntax
update(*others)
set |= other | ...
Example 1: Calling update()
between two sets — A
and B
It’ll update set A
by adding elements found in both sets.
#update() A={1,2,3,4,5} B={4,5,6,7,8} print (A.update(B)) #Output: None print (A) #Output: {1, 2, 3, 4, 5, 6, 7, 8}
A={1,2,3,4,5} B={4,5,6,7,8} A|=B print (A) #Output: {1, 2, 3, 4, 5, 6, 7, 8}
Example 2: Calling update()
between more than two sets
#update() A={1,2,3} B={3,4,5} C={5,6,7} print (A.update(B,C)) #Output: None print (A) #Output: {1, 2, 3, 4, 5, 6, 7} A={1,2,3} B={3,4,5} C={5,6,7} A|=B|C print (A) #Output: {1, 2, 3, 4, 5, 6, 7}
Difference between the update()
method and the |=
operator:
update()
: It’ll accept any iterable as an argument|=
operator: It’ll accept only a set as an argument. Otherwise, it’ll raise aTypeError
.
Example 3: Giving iterable
as an argument in the update()
method
A={1,2,3} #iterable is given as list print (A.update([2,3,4]))#Output:None print (A)#Output:{1,2,3,4} #iterable is given as tuple A={1,2,3} A.update((2,3,4)) print (A)#Output:{1,2,3,4} #iterable is given as range object A={1,2,3} A.update(range(2,5)) print (A)#Output:{1,2,3,4} #iterable is given as a dictionary A={1,2,3} A.update({2:'a',3:'b'}) print (A) #Output:{1, 2, 3}
Example 4: Giving iterable
as an argument for the |=
operator
#iterable is given as tuple A={1,2,3} B=(3,4) A|=B #Output:TypeError: unsupported operand type(s) for |=: 'set' and 'tuple'
‘intersection()’
Return a new set with elements common to the set and the other
. It’s performed by intersection()
or by using the &
operator.
Syntax
intersection(*others)
set & other & ...

Example 1: Find the intersection of two sets — A
and B
It’ll return a new set containing common elements from the set A
and set B
.
A={1,2,3,4,5} B={2,4,6,8} #intersection is performed by intersection() method or & operator print (A.intersection(B))#Output:{2,4} print (A&B)#Output:{2,4}
Example 2: Find the intersection of more than two sets
A={1,2,3,4,5} B={2,4,6,8,10} C={2,4} print (A&B&C)#Output:{2,4} print (A.intersection(B,C))#Output:{2,4}
Difference between the intersection()
method and the &
operator:
intersection()
: It’ll accept any iterable as an argument&
operator: It’ll accept only a set as an argument. Otherwise, it’ll raise aTypeError
.
Example 3: Giving iterable
as an argument in the intersection()
method
A={1,2,3,4,5} #iterable is given as list print (A.intersection([1,4,6]))#Output:{1,4} #iterable is given as tuple print (A.intersection((2,4,6)))#Output:{2,4} #iterable is given as range object print (A.intersection(range(5,10)))#Output:{5} #iterable is given as a dictionary print (A.intersection({1:'a','b':7}))#Output:{1}
Example 4: Giving iterable
as an argument for the &
operator
A={1,2,3,4,5} B=[1,2,3] print (A&B) #Output:TypeError: unsupported operand type(s) for &: 'set' and 'list'
’intersection_update()’
It updates the set, keeping only elements found in it and the other
. It’s performed by using intersection_update()
or by using the&=
operator. The return type is None
. It’ll modify the original set itself.
Syntax
intersection_update(*others)
set &= other & …
Example 1: Find the intersection_update()
between two sets — A
and B
It’ll update set A
by keeping only the elements found in both of the sets.
#intersection_update() A={1,2,3,4,5} B={4,5,6,7,8} print (A.intersection_update(B)) #Output: None print (A) #Output: {4,5}
A={1,2,3,4,5} B={4,5,6,7,8} A&=B print (A) #Output: {4,5}
‘difference()’
Returns a new set with elements in the set that aren’t in the other
. It’s performed by difference()
or by using the -
operator.
Syntax
difference(*others)
set - other - ...

Example 1: Find the difference between two sets — A
and B
It’ll return a new set containing elements from set A
not in set B.
A={1,2,3,4,5} B={2,4,6,8} print (A.difference(B))#Output:{1,3,5} print (A-B)#Output:{1,3,5}
Example 2: Find the difference between more than two sets
A={1,2,3,4,5} B={2,4,6,8,10} C={2,3} print (A-B-C)#Output:{1,5} print (A.difference(B,C))#Output:{1,5}
Difference between the difference()
method and the -
operator:
difference()
: It’ll accept any iterable as an argument-
operator: It’ll accept only a set as an argument. Otherwise, it’ll raise aTypeError
.
Example 3: Giving iterable
as an argument in the difference()
method
A={1,2,3,4,5} #iterable is given as list print (A.difference([1,2,3]))#Output:{4,5} #iterable is given as tuple print (A.difference((1,2,3)))#Output:{4,5} #iterable is given as range object print (A.difference(range(1,4)))#Output:{4,5} #iterable is given as a dictionary print (A.difference({1:'a',2:'b',3:'c'}))#Output:{4,5}
Example 4: Giving iterable
as an argument for the -
operator
A={1,2,3,4,5} B=[1,2,3] print (A-B) #Output:TypeError: unsupported operand type(s) for -: 'set' and 'list'
‘difference_update()’
Removes the element from the set that’s also present in the other
set. It’s performed by using the -=
operator or by using the difference_update()
method. The return type is None
. It’ll modify the original set itself.
Syntax
difference_update(*others)
set -= other | ...

Example 1: Find the difference_update()
between two sets — A
and B
It’ll update set A
by removing elements that are also present in set B
.
A={1,2,3,4,5} B={2,4,6} #Return type is None. print (A.difference_update(B))#Output:None #It will update the original set print (A) #Output: {1,3,5} # difference_update by using -= operator A-=(B) print (A) #Output: {1,3,5}
Example 2: Find the difference_update
between more than two sets
#difference_update() will modify the original set. A={1,2,3} B={1} C={2} #Return type is None. print (A.difference_update(B,C))#Output:None #It will update the original set print (A) #Output: {3} # difference_update by using -= operator A={1,2,3} B={1} C={2} A-=B|C print (A) #Output: {3}
Difference between the difference_update()
method and the -=
operator:
difference_update()
: It’ll accept any iterable as an argument-= operator
: It’ll accept only a set as an argument. Otherwise, it’ll raise aTypeError
.
Example 3: Giving iterable
as an argument in the difference_update()
method
#iterable is given as list A={1,2,3} B=[1] print (A.difference_update(B))#Output:None print (A)#Output:{2,3}
Example 4: Giving iterable
as an argument for -=
operator
A={1,2,3} B=[1] A-=B print (A) #Output: TypeError: unsupported operand type(s) for -=: 'set' and 'list'
‘symmetric_difference()’
Return a new set with elements in either the set or other
but not both. It’s performed by symmetric_difference()
or by using the ^
operator.
Syntax
symmetric_difference(other)
set ^ other

Example 1: Find the symmetric_difference
between two sets — A
and B
It’ll return a new set containing elements from either set A
and set B
but not elements found in both sets.
A={1,2} B={2,3} print (A.symmetric_difference(B))#Output:{1,3} print (A^B)#Output:{1,3}
Example 2: symmetric_difference
is only performed between two sets
The symmetric_difference() method
isn’t supported by multiple sets. If more than two sets are given, it’ll raise a TypeError
.
A={1,2} B={2,3,5} C={3,4} print (A.symmetric_difference(B,C))#Output:TypeError: symmetric_difference() takes exactly one argument (2 given)
But we can find the symmetric_difference
of multiple sets using ^
.
A={1,2} B={2,3,5} C={3,4} print (A^B^C)#Output:{1,4,5}
The difference between the symmetric_difference
method and the&
operator:
symmetric_difference()
: It’ll accept any iterable as an argument. This method doesn’t allow for multiple sets.^
operator: It’ll accept only a set as an argument. Otherwise, it’ll raise aTypeError
. By using the^
operator, you can find thesymmetric_difference
between multiple sets.
Example 3: Giving iterable
as an argument in the symmetric_difference
method
#iterable is given as list A={1,2,3} B=[1] print (A.symmetric_difference(B))#Output:{2,3} #iterable is given as tuple A={1,2,3} B=(1,) print (A.symmetric_difference(B))#Output:{2,3} #iterable is given as range object A={1,2,3} B=range(2) print (A.symmetric_difference(B))#Output:{2,3}
Example 4: Giving iterable
as an argument for the ^
operator
A={1,2,3} B=[1] A^B print (A) #Output: TypeError: unsupported operand type(s) for ^: 'set' and 'list'
‘symmetric_difference_update()’
Updates the set, keeping only elements found in either set but not in both. It’s performed by using symmetric_difference_update()
or by using the ^=
operator. The return type is None
. It’ll modify the original set itself.
Syntax
symmetric_difference_update(other)
set ^= other
Example 1: Find the symmetric_difference_update()
between two sets — A and B
It will update set A
by keeping only elements found in either set but not in both.
#symmetric_difference_update() A={1,2,3,4,5} B={4,5,6,7,8} print (A.symmetric_difference_update(B)) #Output: None print (A) #Output: {1, 2, 3, 6, 7, 8} A={1,2,3,4,5} B={4,5,6,7,8} A^=B print (A) #Output: {1, 2, 3, 6, 7, 8}
‘isdisjoint()’
Returns True
if the set has no elements in common with the other
. Sets are disjointed if and only if their intersection is the empty set.
Syntax
isdisjoint(other)

Example
#Set A and Set B containing common elements A={1,2,3,4,5} B={4,5,6,7,8} print (A.isdisjoint(B))#Output:False #Set A and Set B not containing common elements A={1,2} B={3,4} print (A.isdisjoint(B))#Output:True
‘issubset()’
Test whether every element in the set is in other
.
Syntax:
issubset(other)
set <= other

Example: Check whether set A
is a subset of B
Can be done by the issubset()
method or by using the ≤
operator.
A={1,2,3,4,5} B={4,5,6,7,8} print (A.issubset(B)) #Output: False print (A<=B)#Output: False A={1,2,3} B={1,2,3,4,5} print (A.issubset(B)) #Output: True print (A<=B)#Output: False
Proper subset
Test whether the set is a proper subset of other
— that is, set <= other and set != other
.
Syntax
set < other
Example: Check whether set A
is a proper subset of B
If both sets are equal, this means A.issubsetset(B)
returns True
. But the proper subset A<B
will return False
.
A={1,2,3,4,5} B={4,5,6,7,8} print (A<B)#Output: False A={1,2,3,4,5} B={1,2,3,4,5} print (A<B)#Output: False A={1,2,3} B={1,2,3,4,5} print (A<B)#Output: True
‘issuperset()’
Test whether every element in other
is in the set.
Syntax
issuperset(other)
set >= other

Example: Check whether set A
is a superset of B
Can be done by theissuperset()
method or by using the ≥
operator.
A={1,2,3,4,5} B={4,5,6,7,8} print (A.issuperset(B)) #Output: False print (A>=B)#Output:True A={1,2,3,4,5} B={1,2,3} print (A.issuperset(B)) #Output: True print (A>=B)#Output:True
Proper superset
Test whether the set is a proper superset of other
— that is, set >= other and set != other
.
Syntax
set > other
Example: Check whether set A
is a proper superset of B
.
If both sets are equal, it means A.issuperset(B)
returns True
. But the proper superset A>B
will return False
.
A={1,2,3,4,5} B={4,5} print (A>B)#Output: True A={1,2,3,4,5} B={1,2,3,4,5} print (A>B)#Output: False A={1,2,3} B={1,2,3,4,5} print (A>B)#Output: True
Note

All update methods are not supported by frozenset
. The frozenset
type is immutable and hashable — its contents can’t be altered after it’s created. Since all update methods modify the original set, it’s not supported by frozenset
.
Conclusion
Mathematical set operations can be performed in two ways: by using an operator or by using a method.
The difference is if we use methods, they’ll accept iterable
as an argument. But for operators, arguments should be set only. If not, it’ll raise a TypeError
. All update methods will update the original set, and it’s not supported in frozenset
. Other than update methods, all other methods return a new set.
I hope you have found this article helpful — thanks for reading!
Resources
Make a one-time donation
Make a monthly donation
Make a yearly donation
Choose an amount
Or enter a custom amount
Your contribution is appreciated.
Your contribution is appreciated.
Your contribution is appreciated.
Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee