Set Operations in Python

Similar to Sets in Mathematics, you can also do operations like union, difference in Python as well. This makes Sets unique as well as increases their usefulness. Moreover for doing an operation on sets there does exist some equivalent method as well. Like for doing union of two sets(Putting two sets together) you can either use | operator or use .union() method. Both works the same way.
Below is a table containing Set Operators/Equivalent methods and description for each one.

OperatorEquivalent MethodDescriptionExamples
Intersection
(Symbol &)
.intersection()Makes a new set having only common elements amongst two setsA & B
A.intersection(B)
Union
(Symbol |)
.union()Putting together elements of two setsA | B
A.union(B)
Difference
(Symbol –)
.difference()Makes a newer set of elements which are in A set but not in B setA - B
A.difference(B)
Symmetric Difference
(Symbol ^)
.symmetric_difference()Set of those elements of A, B which are not in both of setsA ^ B
A.symmetric_difference(B)
Disjoint
(No Operator for this in Python)
.isdisjoint()Checks if two sets have common elements or notA.isdisjoint(B)
Issubset
(Symbol <=)
.issubset()Checks if one set is subset of another or notA <= B
A.issubset(B)
Issuperset
(Symbol >=)
.issuperset()Checks if one set is superset of another or notA >= B
A.issuperset(B)

Code Examples of Union(|) or .union() method

# Creating a set
set1 = set({12,3452,1242,3921,2211})

# Crating a set
set2 = set({"ComputerScienceHub.io", "Google", "Facebook", "Microsoft"})

set3 = set1 | set2
print(set3)        # {12,3452,1242,3921,2211,"ComputerScienceHub.io", "Google", "Facebook", "Microsoft"}

set4 = set1.union(set2)
print(set4) 	   # {12,3452,1242,3921,2211,"ComputerScienceHub.io", "Google", "Facebook", "Microsoft"}

# Here set3 and set4 are same as union operator(|) and union method have same functionality

Code Examples of Intersection(&) or .intersection() method

# Creating a set
set1 = set({64, 29, 19, 272, 12})

# Crating a set
set2 = set({64, 2912, 941})

set3 = set1 & set2
print(set3)          # {64} as set1,set2 have only 64 in common

set4 = set1.intersection(set2)
print(set4)          # {64} as set1,set2 have only 64 in common

# set3 and set4 are same as both Intersection Operator and .intersection() method are same

Code Examples of Difference(-) or .difference() method

# Creating a set
set1 = set({64, 29, 19, 272, 12})

# Crating a set
set2 = set({64, 2912, 941})

set3 = set1 - set2
print(set3)          # {29, 19, 272, 12} as these elements are in set1 but not in set2

set4 = set1.difference(set2)
print(set4)          # {29, 19, 272, 12} as these elements are in set1 but not in set2

# set3 and set4 are same as both Difference Operator and .difference() method are same

Code Examples of Symmetric Difference(^) or .symmetric_difference() method

# Creating a set
set1 = set({73, 281, 291, 647, 3})

# Crating a set
set2 = set({64, 2912, 941, 3, 73})

set3 = set1 ^ set2
print(set3)          # {281, 291, 647, 64, 2912, 941} as these elements are not common amongst set1, set2

set4 = set1.symmetric_difference(set2)
print(set4)          # {281, 291, 647, 64, 2912, 941} as these elements are not common amongst set1, set2

# set3 and set4 are same as both Symmetric Difference Operator and .symmetric_difference() method are same

Code Examples of Disjoint or .isdisjoint() method

# Example 1

# Creating a set
set1 = set({737, 92, 291, 913})

# Crating a set
set2 = set({737, 190, 28, 182, 2})

set3 = set1.isdisjoint(set2)
print(set3)          # Returns False as set1,set2 have 737 element in common


# Example 2

# Creating a set
set1 = set({7137, 192, 2912, 9133})

# Crating a set
set2 = set({99, 9213, 952})

set3 = set1.isdisjoint(set2)
print(set3) 		# Returns True as set1, set2 doesn't have any common element

Code Examples of Issubset(<=) or .issubset() method

# Example 1

# Creating a set
set1 = set({737, 2})

# Crating a set
set2 = set({737, 190, 28, 182, 2})

set3 = set1.issubset(set2)
print(set3)          # Returns True as all elements in set1 are there in set2


# Example 2

# Creating a set
set1 = set({737, 2})

# Crating a set
set2 = set({737, 190, 28, 182, 2})

set1 <= set2         # Returns True

Code Examples of Issuperset(>=) or .issuperset() method

# Example 1

# Creating a set
set1 = set({281, 192, 19382, 18492, 241, 13452})

# Crating a set
set2 = set({192, 241})

set3 = set1.issuperset(set2)
print(set3)          # Returns True as set2 have only those elements which are in set1


# Example 2

# Creating a set
set1 = set({281, 192, 19382, 18492, 241, 13452})

# Crating a set
set2 = set({192, 241})

set1 >= set2         # Returns True

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts