Python Difference

In the last lesson, we learned about Python Intersection. Now let’s find the items one set has and another set does not.

Let’s Talk About What One Set Has That the Other Does Not

We have two sets. They each have some items. Some items are in both. Some are only in one.

The difference of two sets is a new set with items that are in the first set but not in the second.

That is the key. It is not symmetric. Order matters.

Think of it like this. We have a list of all our books. Our friend has a list of books they borrowed from us. The difference is the books we have that our friend does not have. That is our_books - friend_books.

If we reverse it, we get the books our friend has that we do not have. That is a different result.

The Difference Operator - in Python

The simplest way to find the difference of two sets is with the - operator.

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result = set1 - set2
print(result)

Output

{1, 2}

Because 1 and 2 are in set1 but not in set2. The items 3 and 4 are in both, so they are not included. And 5 and 6 are only in set2, so they are not included either.

If we reverse the order:

result = set2 - set1
print(result)

Output

{5, 6}

Because now we want items in set2 but not in set1.

That is the important thing. Difference is not symmetric. set1 - set2 is not the same as set2 - set1.

The difference() Method in Python

We can also use the .difference() method. It does the same thing.

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result = set1.difference(set2)
print(result)

Output

{1, 2}

The method can take multiple arguments. It removes items that are in any of the other sets.

set1 = {1, 2, 3, 4, 5}
set2 = {3, 4}
set3 = {5}
result = set1.difference(set2, set3)
print(result)

Output

{1, 2}

So it removes 3, 4 (from set2) and 5 (from set3) from set1. What is left is 1 and 2.

Difference with Other Iterables in Python

Just like union() and intersection(), the difference() method can take any iterable. We can pass a list, a tuple, or a string.

set1 = {1, 2, 3, 4}
list1 = [3, 4, 5]
result = set1.difference(list1)
print(result)

Output

{1, 2}

The list is converted to a set internally, and then the difference is found.

But the - operator only works with sets.

set1 = {1, 2, 3}
list1 = [2, 3, 4]
result = set1 - list1 # Error!

Output

TypeError: unsupported operand type(s) for -: 'set' and 'list'

So if we have a list, we use the .difference() method.

In-Place Difference with difference_update() in Python

The - operator and .difference() method create a new set. The original sets are unchanged.

If we want to modify a set in place, we use the .difference_update() method.

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
set1.difference_update(set2)
print(set1)

Output

{1, 2}

Now set1 has been changed. It no longer contains 3 and 4.

We can also update with multiple sets.

set1 = {1, 2, 3, 4, 5}
set2 = {3, 4}
set3 = {5}
set1.difference_update(set2, set3)
print(set1)

Output

{1, 2}

And difference_update() also accepts any iterable.

set1 = {1, 2, 3, 4}
set1.difference_update([3, 4, 5])
print(set1)

Output

{1, 2}

Python Difference: A Real-Life Example

Let’s say we have a list of all students in a class. And a list of students who passed the exam. We want to find who failed.

all_students = {"Alex", "Sam", "John", "Emma", "David"}
passed = {"Alex", "Sam", "Emma"}
failed = all_students - passed
print(failed)

Output

{'John', 'David'}

Those are the students who did not pass.

Another Example β€” Finding New Items

old_inventory = {"apple", "banana", "orange"}
new_inventory = {"apple", "banana", "orange", "grape", "kiwi"}
added_items = new_inventory - old_inventory
print(added_items)

Output

{'grape', 'kiwi'}

Those are the items that were added to the inventory.

And the other way:

removed_items = old_inventory - new_inventory
print(removed_items)

Output

set()

Because nothing was removed.

Difference with an Empty Set

Difference with an empty set gives us a copy of the original set. Because there is nothing to remove.

set1 = {1, 2, 3}
empty = set()
result = set1 - empty
print(result)

Output

{1, 2, 3}

But if we reverse it:

result = empty - set1
print(result)

Output

set()

Because the empty set has nothing, so removing anything leaves nothing.

Common Mistakes with Python Difference

Mistake 1: Thinking Difference Is Symmetric

set1 = {1, 2}
set2 = {2, 3}
print(set1 - set2) # {1}
print(set2 - set1) # {3}

They are not the same. Order matters.

Mistake 2: Using - with a List

set1 = {1, 2}
list1 = [2, 3]
result = set1 - list1 # Error!

So what do we do? We use the .difference() method.

result = set1.difference(list1)

Mistake 3: Thinking Difference Modifies the Original

set1 = {1, 2, 3}
set2 = {2, 3, 4}
set1 - set2
print(set1) # {1, 2, 3}

The - operator returns a new set. It does not change set1.

If we want to change set1, we use set1.difference_update(set2) or set1 -= set2.

Mistake 4: Confusing Difference with Symmetric Difference

Difference gives items in one set but not the other. Symmetric difference gives items in either set but not both.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
print(set1 - set2) # {1, 2}
print(set1 ^ set2) # {1, 2, 4, 5}

Next, we will learn about Python Set Methods and use built-in tools to update and compare sets.