Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Python Set Difference

Python Set Difference basically performs the difference operation on iterable objects and the result contains the difference between them.

It contains elements that are present in the set (on which the operation is invoked) which are not present in the other sets.

Python Set Difference Venn Diagram

The below Venn diagram provides a much better understanding of the set difference than any text could.

Set Difference
Set Difference

Techniques to find Set Difference

The following are the techniques to find the set difference between multiple sets in python:

  • By using the set difference() method
  • By using the “-” operator
  • By using the difference_update method
  • By using the “-=” operator

1. Python Set Difference using difference() method

The difference() method usually operates on iterables like String, List, Tuples, etc.

The difference() method results in a new set as output which contains all the items from the particular set on which the method is invoked, which are not present in the other set i.e. it creates the difference between two sets.

For example:

If set A = {1, 9, 5, 7} and set B = {2, 8, 9, 7}

Then, set A difference set B would contain all the elements that are present in set A but not in set B i.e. result = {1, 5}.

Note: If any iterable other than set is passed to the difference() method as an argument, then the method first converts the iterable object to sets and then performs the operation on it.

Syntax:

Set.difference(iterable_object) = Set Object

Example:

Set_A = {1, 3, 5, 7, 9}

Set_B = {2, 4, 6, 7, 9}

Result = Set_A.difference(Set_B)
print(Result);

input_list = [1, 2, 3, 4, 5, 6, 7] #list
Display = Set_A.difference(input_list)
print(Display)

Output:

{1, 3, 5}
{9}

2. Using “-” operator for the Set difference in Python

The “-” operator can also be used to perform the set difference operation on elements. It serves the same purpose as the difference() method.

The only difference between the “-” operator and the difference() method is that the “-” operator works only on set elements, while the latter works on any iterable object.

Syntax:

SetA - SetB = Resultant_Set

Example:

Set_A = {1, 3, 5, 7, 9}

Set_B = {2, 4, 6, 7, 9}

Result = Set_A - Set_B 
print(Result)

Output:

{1, 3, 5}

3. Using difference_update() method

The difference_update() method also returns the difference between set elements from iterable objects like String, Lists, Tuples, etc.

In difference_update() method, the result of the difference operation is not stored in a newly created set, rather it updates the same set on which the operation was performed.

Syntax:

Set.difference_update(iterable_object)

Example:

Set_A = {1, 3, 5, 7, 9}

Set_B = {2, 4, 6, 7, 9}

print("Set_A before the difference operation: ", Set_A)

Set_A.difference_update(Set_B) 

print("Set_A difference Set_B: ", Set_A);

Output:

Set_A before the difference operation:  {9, 1, 3, 5, 7}
Set_A difference Set_B:  {1, 3, 5}

4. Using “-=” operator

The “-=” operators works in a similar way like the difference_update method i.e. it performs the difference operations on the set elements and updates the result within the same set on which the difference operation was performed.

Note: The “-=” operator operates only on set objects.

Syntax:

Set_A -= Set_B

Example:

Set_A = {1, 3, 5, 7, 9}

Set_B = {2, 4, 6, 7, 9}

print("Set_A before the difference operation: ", Set_A)

Set_A -= Set_B

print("Set_A difference Set_B: ", Set_A);

Output:

Set_A before the difference operation:  {9, 1, 3, 5, 7}
Set_A difference Set_B:  {1, 3, 5}

Conclusion

Thus, in this article, we have understood the different ways to find the difference of set elements in Python.


References