Mathematical Set Operations in Python
By: Indhumathy Chelliah
Tags: Data Science, Python, DevOps, Programming, Python3

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 | ...

‘union()’

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

: It’ll accept any iterable as an argument | operator: It’ll accept only a set as an argument. Otherwise, it’ll raise a TypeError .

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

: It’ll accept any iterable as an argument |= operator: It’ll accept only a set as an argument. Otherwise, it’ll raise a TypeError .

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 & ...

‘intersection()’

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

: It’ll accept any iterable as an argument & operator: It’ll accept only a set as an argument. Otherwise, it’ll raise a TypeError .

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 - ...

‘difference()’

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

: It’ll accept any iterable as an argument - operator: It’ll accept only a set as an argument. Otherwise, it’ll raise a TypeError .

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 | ...

‘difference_update()’

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

: It’ll accept any iterable as an argument -= operator : It’ll accept only a set as an argument. Otherwise, it’ll raise a TypeError .

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

symmetric_difference()

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.

: 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 a TypeError . By using the ^ operator, you can find the symmetric_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.