Python Programming Language: Comparing Sets

Jesse L
3 min readJun 30, 2022

Hi everyone, welcome back. In these examples, we will be going over how to compare sets in Python. Sets can be used to store a collection of data. A set in Python does not allow duplicate values. The elements within a set are immutable, though elements can still be added or removed. The elements within a set are unordered and unindexed. With this introduction out of the way, let’s get into it.

isdisjoint() Function

The isdisjoint() function can be used to determine whether or not two sets have an intersection or not. Let’s see an example:

mySet = {1, 2, 3}
mySet2 = {4, 5, 6}
print(mySet.isdisjoint(mySet2))
Output:
True

In the example above, we can see that we used the isdisjoint() function on our two sets and judging by the output, we can verify that our two sets are disjoint. This means that there is no shared value between the two sets. Let’s see what happens if we do have a shared value:

mySet = {1, 2, 3}
mySet2 = {3, 4, 5}
print(mySet.isdisjoint(mySet2))
Output:
False

Looking at the example above, we can tell by the output that our two sets are not disjoint, meaning that there is at least one shared value between the two sets.

issubset() Function

--

--

Jesse L

Hi, I'm a passionate technology enthusiast and lifelong learner. Beyond my technical pursuits, I'm also passionate about sharing my enthusiasm with others.