Python Set difference() Method
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().
| Syntax | a.difference(*others) or a - b |
|---|---|
| Parameters | Zero or more iterables |
| Returns | A new set |
| Changes the original? | No (difference_update() does) |
| Time complexity | About 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.
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.
a = {1, 2, 3, 4}
b = {3, 4, 5}
print(sorted(a - b))
print(sorted(b - a))difference() vs difference_update() vs -
| Form | Accepts | Result | Modifies the original? |
|---|---|---|---|
a.difference(b) | Any iterable | New set | No |
a - b | Sets only | New set | No |
a.difference(b, c) | Several iterables | New set without items of b or c | No |
a.difference_update(b) | Any iterable | None | Yes, removes the items |
a -= b | Sets only | Updates a | Yes |
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.
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.
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)✓ 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)✓ 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 →