ReferenceSet methods

Python Set difference() Method

Short answer
a.difference(b) returns a new set of the elements that are in a but not in b. The order of the sets matters, and the originals are not changed.

Difference answers "what is left after I take away the other group?" For instance, which students have not submitted homework, or which items in the catalogue are not yet in the cart. It is one of the core set operations.

Unlike union and intersection, difference is not symmetric: a - b and b - a usually give different results. If you want what is in exactly one of the two, use symmetric_difference().

Quick facts
Syntaxa.difference(*others) or a - b
ParametersZero or more iterables
ReturnsA new set
Changes the original?No (difference_update() does)
Time complexityAbout O(len(a)) for one other set

How set.difference() works

Python keeps each element of a that is not found in the other sets.

Python
all_students = {"Asha", "Ravi", "Meera", "Kabir"}
submitted = {"Ravi", "Kabir"}
print(sorted(all_students.difference(submitted)))
print(sorted(all_students - submitted))

Why the order of the sets matters

Only elements of the first set can appear in the result. Swapping the sets answers a different question.

Python
a = {1, 2, 3, 4}
b = {3, 4, 5}
print(sorted(a - b))
print(sorted(b - a))

difference() vs difference_update() vs -

FormAcceptsResultModifies the original?
a.difference(b)Any iterableNew setNo
a - bSets onlyNew setNo
a.difference(b, c)Several iterablesNew set without items of b or cNo
a.difference_update(b)Any iterableNoneYes, removes the items
a -= bSets onlyUpdates aYes

Examples you can run

Press Run, then edit the code and run it again.

Who has not submitted homework

Subtract the submitters from the full class list.

Who has not submitted homework
all_students = {"Asha", "Ravi", "Meera", "Kabir"}
submitted = {"Ravi", "Kabir"}
pending = all_students.difference(submitted)
print(sorted(pending))
Expected output['Asha', 'Meera']

Subtract several collections at once

One call can remove items found in a list and a set together.

Subtract several collections at once
catalog = {"pen", "book", "ruler", "bag"}
in_cart = {"pen"}
owned = ["bag"]
print(sorted(catalog.difference(in_cart, owned)))
print(len(catalog))
Expected output['book', 'ruler']
4

Common mistakes

Swapping the two sets

✗ Wrong

all_students = {"Asha", "Ravi"}
submitted = {"Ravi"}
print(submitted - all_students)
set() (not the pending students)

✓ Right

all_students = {"Asha", "Ravi"}
submitted = {"Ravi"}
print(all_students - submitted)

Put the larger "everything" set first: all_students - submitted.

Expecting the original to change

✗ Wrong

a = {1, 2, 3}
a.difference({1})
print(a)
{1, 2, 3} (the result was discarded)

✓ Right

a = {1, 2, 3}
a.difference_update({1})
print(sorted(a))

Assign the result or use difference_update().

Python Set difference() Method: questions people ask

How do I find items in one list but not in another in Python?

Convert them to sets and subtract: set(a) - set(b). Use sorted() if you need an ordered list back.

Is set difference commutative?

No. a - b and b - a are generally different. If you want both sides at once, use symmetric_difference().

What is the difference between difference() and difference_update()?

difference() returns a new set and keeps the original, while difference_update() removes the items from the original and returns None.

Keep learning

Step-by-step lessons:
Python Set OperationsPython Sets

Related:
Python Set symmetric_difference() MethodPython Set union() MethodPython Set intersection() MethodPython Set remove() Method

More in set methods:
Python Set add() MethodPython Set remove() MethodPython Set discard() MethodPython Set pop() MethodPython Set union() MethodPython Set intersection() MethodPython Set symmetric_difference() MethodPython Set update() Method See all →