Python Set difference() Method
set.difference() returns the values that are in one set but not in another.
This is useful when you want to find items that exist in the first set and remove anything that also appears in one or more other sets.
Quick answer
a = {1, 2, 3, 4}
b = {3, 4, 5}
result = a.difference(b)
print(result)
Output:
{1, 2}
difference() returns a new set with items from a that are not in b.
What difference() does
The difference() method compares sets and keeps only the values from the first set that do not appear in the other set.
Key points:
difference()returns items that are in the first set but not in the other set- It creates a new set
- It does not change the original set
- It is useful for finding removed, missing, or unique values
If you are new to sets, see Python sets explained.
Basic syntax
Use this form:
set1.difference(set2)
You can also compare against more than one set:
set1.difference(set2, set3)
In that case, Python removes any value found in any of the other sets.
Example:
a = {1, 2, 3, 4, 5}
b = {2, 3}
c = {5}
result = a.difference(b, c)
print(result)
Output:
{1, 4}
Here, Python starts with a and removes anything found in b or c.
Return value
difference() always returns a new set.
Important details:
- The return value is a new set
- The result may be an empty set if all items are removed
- The result is still a set, even if nothing remains
Example:
a = {1, 2}
b = {1, 2, 3}
result = a.difference(b)
print(result)
print(type(result))
Output:
set()
<class 'set'>
An empty set is shown as set(), not {}. In Python, {} means an empty dictionary.
Simple example
Here is a basic example with two sets:
fruits_a = {"apple", "banana", "orange"}
fruits_b = {"banana", "orange", "grape"}
result = fruits_a.difference(fruits_b)
print(result)
print(fruits_a)
print(fruits_b)
Possible output:
{'apple'}
{'banana', 'orange', 'apple'}
{'banana', 'orange', 'grape'}
What happens here:
- Start with
fruits_a - Remove any values also found in
fruits_b - Only
"apple"remains - The original sets stay unchanged
Notice that the printed order may look different. Sets do not keep items in a fixed order.
Using difference() with multiple sets
You can pass two or more sets into difference().
Python removes any value from the first set if that value appears in any of the other sets.
numbers = {1, 2, 3, 4, 5, 6}
group_a = {2, 4}
group_b = {5}
group_c = {6, 7}
result = numbers.difference(group_a, group_b, group_c)
print(result)
Output:
{1, 3}
This is helpful when you want to compare one main set against several groups at once.
difference() vs difference_update()
These two methods are similar, but they work differently.
difference()
- Returns a new set
- Does not change the original set
a = {1, 2, 3, 4}
b = {3, 4}
result = a.difference(b)
print(result)
print(a)
Output:
{1, 2}
{1, 2, 3, 4}
difference_update()
- Changes the original set
- Does not return a new set
a = {1, 2, 3, 4}
b = {3, 4}
a.difference_update(b)
print(a)
Output:
{1, 2}
Use difference() when you want to keep the original data.
For more on the in-place version, see Python set difference_update() method.
difference() vs the - operator
The - operator does the same kind of comparison as difference() for two sets.
a = {1, 2, 3, 4}
b = {3, 4}
print(a.difference(b))
print(a - b)
Output:
{1, 2}
{1, 2}
Both lines give the same result.
Why use difference()?
- It is often clearer for beginners
- The method name explains what the code is doing
- It easily supports multiple sets
Example with multiple sets:
a = {1, 2, 3, 4, 5}
b = {2}
c = {4}
print(a.difference(b, c))
Output:
{1, 3, 5}
You cannot write that in the same simple way with a single - operator expression.
Common beginner mistakes
Here are some common problems when using difference().
Expecting the original set to change
This does not change a:
a = {1, 2, 3}
b = {3}
a.difference(b)
print(a)
Output:
{1, 2, 3}
If you want to keep the result, save it:
a = {1, 2, 3}
b = {3}
a = a.difference(b)
print(a)
Output:
{1, 2}
Expecting items from the second set instead of the first set
a.difference(b) means:
- keep items from
a - remove anything also in
b
It does not mean “show me what is unique in b”.
Example:
a = {1, 2, 3}
b = {3, 4, 5}
print(a.difference(b))
print(b.difference(a))
Output:
{1, 2}
{4, 5}
The order of the sets matters.
Comparing lists instead of sets
difference() is a set method, not a list method.
This will fail:
a = [1, 2, 3]
b = [3, 4]
# a.difference(b)
Convert lists to sets first:
a = [1, 2, 3]
b = [3, 4]
result = set(a).difference(set(b))
print(result)
Output:
{1, 2}
Thinking the output order will match the input order
Sets are unordered, so printed output may appear in a different order.
a = {"cat", "dog", "bird"}
b = {"dog"}
print(a.difference(b))
You should focus on which values are present, not their position.
Trying to use duplicate values in a set example
Sets automatically remove duplicates.
a = {1, 1, 2, 2, 3}
b = {3}
print(a)
print(a.difference(b))
Output:
{1, 2, 3}
{1, 2}
The duplicates are already gone before difference() runs.
Quick debugging checks
If your result is not what you expect, print the values you are working with:
a = {1, 2, 3, 4}
b = {3, 4, 5}
print(a)
print(b)
print(a.difference(b))
print(type(a.difference(b)))
print(len(a.difference(b)))
These checks help you confirm:
- what is in each set
- what result Python creates
- that the result is a set
- how many items remain
FAQ
Does difference() change the original set?
No. It returns a new set and leaves the original set unchanged.
What is the difference between difference() and difference_update()?
difference() returns a new set. difference_update() modifies the original set.
Can I use difference() with more than two sets?
Yes. You can pass multiple sets, and Python removes items found in any of them.
Why is my output in a different order?
Sets are unordered collections, so item order is not guaranteed.