How to Find Set Difference in Python

Learn how to find the set difference between two Python sets using the difference() method and the difference (-) operator.



How to Find Set Difference in Python
Image by Author

 

In Python, sets are built-in data structures that store an unordered collection of non-repeating and immutable elements. You can perform common operations from set theory such as union, intersection, and set difference on Python sets.

This tutorial will teach you how to compute set difference in Python. You will learn how to use the built-in set method  difference() and - operator to find the set difference, and how to debug common errors during the process.

Let’s get started.

 

What Is Set Difference?

 

Before computing set difference on Python sets, let's quickly review the set difference operation. 

Given two sets A and B, we can define the following:

  • A - B: (read as A  difference B) is the set of all elements that are present in set A but not in set B.
  • B - A: (read as B  difference A) is the set of all elements that are present in set B but not in set A. 

Set difference is not a commutative operation. Therefore, A - B is not the same as A - B unless the sets A and B are equal, that is, A and B contain the same elements.

We can see this from the simple example below:

 

What Is Set Difference?
Image by Author

 

In this example:

  • Set A: {2,1,3,12,7}
  • Set B: {21,3,12,10}

Therefore, A - B is {1,2,7}, the set of elements present only in A. And B - A is {21,10}, the set of elements present only in B.

 

Set Difference Using the difference() Method

 

Let us define fruits and to_eat, two Python sets containing strings as the individual elements.

 

Set Difference Using the difference() Method
Image by Author

 

fruits = {"apples","oranges","berries","cherries"}
to_eat = {"apples","cereals","berries","bread"}

 

Now to find fruits - to_eat, let’s call the difference() method on the fruits set with to_eat as the argument:

print(fruits.difference(to_eat))
Output >> {'cherries', 'oranges'}

 

Similarly, to find to_eat - fruits, let’s call the difference() method on the to_eat set, as shown:

print(to_eat.difference(fruits))
Output >> {'cereals', 'bread'}

 

Set Difference Using the Difference Operator (-)

 

We can also use the difference operator (-) to find the set difference.

Let’s revisit the same example: For the sets fruits and to_eat, using the difference operator (-) performs an equivalent operation and returns the same results:

print(fruits - to_eat)
Output >> {'cherries', 'oranges'}

 

print(to_eat - fruits)
Output >> {'cereals', 'bread'}

 

Debugging Common Set Difference Errors

 

In the examples we have coded so far, we computed the difference between two Python sets. But did not emphasize how the difference() method works differently than the difference operator. 

You can call the difference() method on any valid set object. However, you can pass in one or more sets or other Python iterables. Here’s an example. 

Let’s define set1, list1, list2, and list3:

set1 = {1,2,3,4,5}
list1 = [2,4,6]
list2 = [3,6,9]
list3 = [10,11]

 

Now we can call the difference() method on the set1 and pass in list1, list2, and list3 in the method call. 

print(set1.difference(list1,list2,list3))
Output >> {1, 5}

 

The difference method, in this case, returns the elements {1,5} from the set that are present only in set1 and not in list1, list2, and list3. 

However, if you try doing the same thing with the difference operator, you will run into a TypeError exception. As - is a binary operator that operates on two operands, let’s concatenate the three lists, and then try computing the set difference:

>>> set1 - (list1 + list2 + list3)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for -: 'set' and 'list'

 

As seen above, unlike the difference() method, the - operator only works with Python sets. So you’ll have to cast other iterables into Python sets before computing the difference, as shown: 

print(set1 - set(list1 + list2 + list3))
Output >> {1, 5}

 

Conclusion

 

Let us quickly review what we’ve learned in this tutorial. To find the set difference, we can use either the difference() method on a Python set or the - operator. While the difference() method call acts on a set and takes in one or more Python iterables as the arguments, the - operator lets you perform set difference operation between two Python sets only. If you’re looking to learn Python, check out this list of free resources.

 
 
Bala Priya C is a technical writer who enjoys creating long-form content. Her areas of interest include math, programming, and data science. She shares her learning with the developer community by authoring tutorials, how-to guides, and more.