In this Python tutorial, we’ll learn how to easily use Python set difference method. Multiple examples will be used for detailed demonstration of the role of difference method in Python set.
Introduction: Python Set Difference Method
This method is used to return a set which consist of items that are present in first Python set and not present in the second set. Don’t worry we’ll understand it practically with proper examples.
Let’s first understand its syntax. Then we’ll implement it using practical Python code examples.
Syntax of Difference Method
setA.difference(setB)
- We’ve two Python sets. Set A and set B.
- The difference method takes a single argument. A set will be passed to it. The resulting set will not include items that are present in that set (e.g. set B in this case).
- This method returns a set which include items of set A that are not present in set B.
Implementing Python Set Difference Method (Detailed Examples)
Below examples will implement the difference method in different type of programs for better understanding.
Example 1: Find Difference Between Two Sets
a={1,2,4,5} b={2,6,5,0,4,7} res=a.difference(b) print(res)
Output
{1}
- We’ve created 2 Python sets.
- After that, we’ve applied difference method on it. In this case, set A is the original set and set B is the specified set.
- Difference method will return a set of items that are present in set A but not present in set B.
- As a result, 1 is returned which means 1 is not present in the specified set (B).
Let’s now make the set B original and set A a specified set. See below:
res=b.difference(a) print(res)
Output
{0, 6, 7}
In this case, the difference method will return a set of items that are present in set B but not present in set A.
Example 2: Empty Set Returned By Difference Method
Let’s see what is returned if no unique items are found in first set. See below:
a={8,7,6,'Zee'} b={'Zee',8,6,7,9,3,5} result=a.difference(b) print(result)
Output
set()
We can see that set() is returned which denotes an empty set. Reason is that all the items of set A were also present in set B so no unique items were found in set A. If we specify set B as an original set then we can see some unique items. See below:
result=b.difference(a) print(result)
Output
{9, 3, 5}
Example 3: Return Value of Python Set Difference Method
aa = {'Zeeshan','Yameen','Saqib',2,5,7,(2,7),8.5} bb = {'Saqib','Usman',45.7,2,5,7} result = aa.difference(bb) print(result)
Output
{8.5, 'Zeeshan', 'Yameen', (2, 7)}
- We’ve created two sets and then we’ve applied Python set difference method on it.
- In this case, set(aa) is the original set and set(bb) is the specified Python set.
- We’ve created a variable which will store the value returned by difference method of Python set.
- Finally, we’ve printed value of this variable using print function of Python.
- We can see that a set has been returned by difference method which includes all the items that were present in set(aa) but not in set(bb).
- It means difference method returns a set which consists of unique items of original set(set aa in this case).
Example 4: Find Difference Between list Of Sets Using Python For Loop
lstA = [ {1,2,3},{9,8,77},{'y','u','pp',7} ] lstB = [ {5,1,3},{2,22,'Zee',774},{'123',(2,4),'u',7} ] resList = [] for var1,var2 in zip( lstA,lstB ): resList.append( var1.difference(var2) ) print(resList)
Output
[ {2}, {8, 9, 77}, {'y', 'pp'} ]
- In the above program, we’ve created 2 Python lists. These lists have sets as their items.
- We’ve created an empty list as well which will be used to store the resulting list of sets.
- Python for loop is used and we’ve used two variables to store items or lists in each iteration. First variable will store items of first list and second variable will store items of second list. It is order sensitive. For instance, in the case of (lstB,lstA), first variable will store the items of list B and second variable will store the items of list A.
- We’ve used append method of Python list to store the resulting sets in the new list after the difference method is applied on them.
- Finally, we’ve displayed the resulting list of sets. It can be seen in the above output block.
So this is the proper use of Python set difference method. Do ask if you still have any questions regarding Python set difference method. We’ll be more than happy to answer all.
Also, if you have learned something new from this article then feel free to share it with other Python developers.
Conclusion
In conclusion, we now have a practical understanding of Python set difference method. We’d be more than happy to receive your informative feedback on this post.
You can discover more concepts about Python programming by paying a visit to our other posts. Thank you for reading this one.