Sets are used to store unique objects. Sometimes, we might need to find the elements in a set that are not present in another given set. For this, we use the set difference operation. In this article, we will discuss what is set difference is. We will also discuss approaches to find the set difference in python.
What is the Set Difference?
When we are given two sets A and B. The set difference (A-B) is a set consisting of all the elements that belong to A but are not present in set B.
Similarly, the set difference (B-A) is a set consisting of all the elements that belong to B but are not present in set A.
Consider the following sets.
A={1,2,3,4,5,6,7}
B={5,6,7,8,9,10,11}
Here, set A-B will contain the elements 1,2,3, and 4 as these elements are present in set A but do not belong to set B. Similarly, set B-A
will contain the elements 8,9,10,11 as these elements are present in the set B but do not belong to set A .
Let us now discuss approaches to find set difference in python.
How to Find The Set Difference in Python?
Given the sets A and B, if we want to find the the set difference A-B, we will first create an empty set named output_set
. After that, we will traverse set A using a for loop. While traversal, we will check for each element if they are present in the set B or not. If any element in set A doesn’t belong to the set B, we will add the element to the output_set
using the add()
method.
After execution of the for loop, we will get the set difference A-B in the output_set
. You can observe this in the following example.
A = {1, 2, 3, 4, 5, 6, 7}
B = {5, 6, 7, 8, 9, 10, 11}
output_set = set()
for element in A:
if element not in B:
output_set.add(element)
print("The set A is:", A)
print("The set B is:", B)
print("The set A-B is:", output_set)
Output:
The set A is: {1, 2, 3, 4, 5, 6, 7}
The set B is: {5, 6, 7, 8, 9, 10, 11}
The set A-B is: {1, 2, 3, 4}
If we want to find the the set difference B-A, we will traverse set B using a for loop. While traversal, we will check for each element if they are present in the set A or not. If any element in set B doesn’t belong to the set A, we will add the element to the output_set
using the add()
method.
After execution of the for loop, we will get the set difference B-A in the output_set
. You can observe this in the following example.
A = {1, 2, 3, 4, 5, 6, 7}
B = {5, 6, 7, 8, 9, 10, 11}
output_set = set()
for element in B:
if element not in A:
output_set.add(element)
print("The set A is:", A)
print("The set B is:", B)
print("The set B-A is:", output_set)
Output:
The set A is: {1, 2, 3, 4, 5, 6, 7}
The set B is: {5, 6, 7, 8, 9, 10, 11}
The set B-A is: {8, 9, 10, 11}
Find Set Difference Using The difference() Method in Python
Python provides us with the difference()
method to find the set difference. The difference()
method, when invoked on set A, takes set B as input argument, calculates the set difference, and returns a set containing the elements in the set (A-B). You can observe this in the following example.
A = {1, 2, 3, 4, 5, 6, 7}
B = {5, 6, 7, 8, 9, 10, 11}
output_set = A.difference(B)
print("The set A is:", A)
print("The set B is:", B)
print("The set A-B is:", output_set)
Output:
The set A is: {1, 2, 3, 4, 5, 6, 7}
The set B is: {5, 6, 7, 8, 9, 10, 11}
The set A-B is: {1, 2, 3, 4}
Conclusion
In this article, we have discussed how to find the set difference in python. To learn more about sets, you can read this article on set comprehension in python. You might also like this article on list comprehension in python.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.