Python Set Difference: A Complete Guide

Python set difference is a function that returns the elements of the first set that aren’t found in the second set. Here’s how it works with code.

Written by Dario Radečić
Published on Dec. 12, 2024
Developer writing Python code
Image: Shutterstock / Built In
Brand Studio Logo

With Python set difference, you can easily find the difference between two or more sets. That means it only returns the distinct values that are unique to the first set.

Python Set Difference Explained

Python set difference function returns the element(s) of the first set that aren’t found in the second set. Its syntax is as follows:

# Difference between two sets
set1.difference(set2)

# Difference between multiple sets
set1.difference(set2, set3, ...)

You’ll get a much more in-depth understanding in this article, so continue reading.

 

What Is Python Set Difference?

So, what is Python set difference? That’s what we’ll answer in this section. You’ll get a complete understanding of the definition, syntax and return values through visual examples.

Python Set Difference Definition and Usage

The set difference function returns the element(s) of the first set that aren’t found in the second set. The same logic applies to finding the difference between multiple sets. For simplicity’s sake, we’ll work with two in the examples below.

Take a look at the following two sets: A and B:

Two sets with programming languages
Two sets with programming languages. | Image: Dario Radečić

Calculating a difference between these sets means we’ll get a new set with a single element,  PHP. Why? Because it’s the only element of set A that isn’t found in set B:

Set difference in action
Set difference in action. | Image: Dario Radečić

Similarly, B-A would result in Ruby, as that element is specific to set B. Python set difference is often represented with a Venn diagram. Here’s what it looks like:

Set difference as a Venn diagram
Set difference as a Venn diagram. | Image: Dario Radečić

Elements Python and JavaScript are common to both sets. We only care about unique elements from the first set when calculating the set difference, that’s why only PHP is returned in the new set.

What does the difference method do in Python, and how do you find the difference in sets in Python? Let’s go over the syntax to answer that question.

More on Python3 Ways to Add Rows to a Pandas DataFrame

 

Python Set Difference Syntax

# Difference between two sets
set1.difference(set2)

# Difference between multiple sets
set1.difference(set2, set3, ...)

Where:

  • set1: The iterable to find the difference from.
  • set2, set3: Other sets used to “disqualify” elements from set1.

Return Value

The difference function returns a new set which is the difference between the first set and all other sets passed as arguments, but only if the sets or iterable objects were passed to the function.

If no arguments were passed into the difference() function, a copy of the set is returned.

 

Python Set Difference Function Example

We’ll declare two sets, just as we did in the first image

  • A: Contains Python, JavaScript, and PHP.
  • B: Contains Python, JavaScript, and Ruby.

As you can see, the first two languages are present in both sets. Calculating the difference as A-B should return a new set with only PHP. Likewise, B-A returns a new set with only Ruby:

A = {'Python', 'JavaScript', 'PHP'}
B = {'JavaScript', 'Python', 'Ruby'}

Output:

A - B = {'PHP'}
B - A = {'Ruby'}

If you don’t specify any parameters to the difference function, a copy of the set is returned:

print(f"A - B = {A.difference()}")

Output:

A - B = {'JavaScript', 'PHP', 'Python'}

You can verify it was copied by printing the memory address:

A = {'Python', 'JavaScript', 'PHP'}
A_copy = A.difference()

print(hex(id(A)))
print(hex(id(A_copy)))

Output:

0x1107d3f20
0x1107d3d60

You won’t see the identical values, and that’s not the point. The important thing is that they’re different, indicating the set was copied to a different memory address.

Let’s now explore a shorter way to get the set difference by using the minus operator.

 

Python Set Difference Using the Minus(-) Operator

You don’t have to call the difference() function every time. You can use the minus (-) operator instead:

A = {'Python', 'JavaScript', 'PHP'}
B = {'JavaScript', 'Python', 'Ruby'}

print(f"A - B = {A - B}")

Output:

A - B = {'PHP'}

Everything else remains the same. Just remember that both operands must be of type set.

A tutorial on Python set difference. | Video: Better Data Science

More on PythonUnderstanding Duck Typing in Python

 

Python Set Difference Common Mistakes

You’re likely to encounter errors when you first start working with sets. These are common, but usually easy to debug.

AttributeError: ’List’ Object Has No Attribute ‘Difference’

This is the most common type of error and it occurs when you try to call the set difference() function on the wrong data type. Only sets have access to this function.

Here’s an example, an exception is raised if you use lists:

A = ['Python', 'JavaScript', 'PHP']
B = ['JavaScript', 'Python', 'Ruby']

print(f"A - B = {A.difference(B)}")

Output:

No attribute error
No attribute error. | Image: Dario Radečić

Make sure both are of type set, and you’ll be good to go.

TypeError: Unsupported Operand Type(s) for -: ‘Set’ and ’List’

This error occurs when you try to use shorthand notation (-) on invalid data types. Both must be sets for - to work. Here’s an example:

A = {'Python', 'JavaScript', 'PHP'}
B = ['JavaScript', 'Python', 'Ruby']

print(f"A - B = {A - B}")

Output:

Unsupported operand types error
Unsupported operand types error. | Image: Dario Radečić

As you can see, A is a set and B is a list, so the - doesn’t work.

Python set difference is simple to understand. We covered the basics and built our way toward more advanced concepts and typical errors you may encounter at some point.

Frequently Asked Questions

The set() method in Python is used to convert any iterable data type to an element with distinct elements set.

Sets are collections in which repetition and order are ignored. So, no, sets can’t have duplicates.

Set difference is not commutative, A-B is not the same as B-A. Here’s an example:

A = {1, 2, 3}
B = {3, 4, 5}

print(f"A - B = {A.difference(B)}")
print(f"B - A = {B.difference(A)}")

Output:

A - B = {1, 2}
B - A = {4, 5}
Explore Job Matches.