# Python Set difference()

Python’s set.difference(sets) method creates and returns a new set containing all elements of this set, except the ones in the given set argument or arguments. The resulting set has at most as many elements as this set.

Here’s a minimal example where we return a new set with the elements from an existing set after removing the elements 1 and 2 from the new set:

>>> s = {1, 2, 3}
>>> t = {1, 2}
>>> s.difference(t)
{3}

## Syntax

Let’s dive into the formal syntax of the set.difference() method.

set.difference(*sets)

## Return Value of Set difference()

The return value of set.difference() is a new set containing all elements of the set it is called on, except the ones in the given set argument or arguments. The resulting set has at most as many elements as any other set given in the argument list.

There are some subtleties you need to understand regarding the set difference function. Let’s dive into them by example!

The straightforward example is to calculate the difference of a set with another subset:

>>> {1, 2, 3}.difference({1})
{2, 3}

But what if you’d invert this and calculate the difference of a subset and a superset? In this case, the result is the empty set after removing all elements from the new set:

>>> {1}.difference({1, 2, 3})
set()

Can you compute the difference between a set and an empty set? Sure! The return value is the original set, copied.

>>> {1, 2, 3}.difference(set())
{1, 2, 3}

What if there’s an overlap between both sets but both sets have elements that are not contained in the other one? In this case, you’d remove all elements in the overlap from the new set—after copying all elements from the original set into it.

>>> {1, 2, 3}.difference({2, 3, 4})
{1}

## Set Difference Multiple Set Arguments

You can compute the difference between an original set and an arbitrary number of set arguments. In this case, the return value will be a set that contains only elements that are members of only the original set. The result is the new set with those elements.

Here’s an example:

>>> {1, 2, 3, 4, 5, 6}.difference({1, 2}, {3, 4}, {1, 3, 5})
{6}

Only the element 6 is not a member of any of the set arguments.

## Python Set Difference vs Minus

A much more concise way to write the set difference is the overloaded minus operator "-". When applied to two sets s and t, the result of s - t is the same as calling s.difference(t). It computes the difference of all elements in the original set except the elements in the second set.

Here’s a basic example:

>>> {1, 2, 3, 4}.difference({3, 4, 5})
{1, 2}
>>> {1, 2, 3, 4} - {3, 4, 5}
{1, 2}

You can see that this minus notation is more concise and more readable at the same time. Therefore, it is recommended to use the minus operator over the set.difference() method.

To compute the set difference of multiple sets with the minus operator, chain together multiple difference computations like this: s0 - s1 - s2 - ... - sn.

>>> {1, 2, 3, 4, 5} - {1, 2} - {2, 3} - {3, 4}
{5}

You don’t need to import any library to use the minus operator—it is built-in.

## Set difference() vs difference_update()

The set.difference() method returns a new set whereas the set.difference_update() operates on the set it is called upon and returns None.

• s.difference(t) – Create and return a new set containing all elements of this set except the ones in the given set arguments.
• s.difference_update(t) – Remove all elements from this set that are members of any of the given set arguments.

Here’s an example that shows the difference between both methods:

>>> s = {1, 2, 3}
>>> t = s.difference({1, 2})
>>> s
{1, 2, 3}

And the set.difference_update() updates on an existing set s and doesn’t return anything:

>>> s = {1, 2, 3}
>>> s.difference_update({1, 2})
>>> s
{3}

## What is the Time Complexity of Set difference()?

The runtime complexity of the set.difference() function on a set with n elements and a set argument with m elements is O(n) because you need to check for each element in the first set whether it is a member of the second set. Checking membership is O(1), so the runtime complexity is O(n) * O(1) = O(n). In fact, if the second set is smaller, the runtime complexity is smaller as well, i.e., m<n –> set difference is O(m).

You can see this in the following simple experiment where we run the set method multiple times for increasing set sizes:

I ran this experiment on my Acer Aspire 5 notebook (I know) with Intel Core i7 (8th Gen) processor and 16GB of memory. Here’s the code of the experiment:

import matplotlib.pyplot as plt
import random
import time

sizes = [i * 10**5 for i in range(50)]
runtimes = []

for size in sizes:
s = set(range(size))
t = set(range(0, size, 2))

# Start track time ...
t1 = time.time()
s.difference(t)
t2 = time.time()
# ... end track time

runtimes.append(t2-t1)

plt.plot(sizes, runtimes)
plt.ylabel('Runtime (s)')
plt.xlabel('Set Size')

plt.show()

## Other Python Set Methods

All set methods are called on a given set. For example, if you created a set s = {1, 2, 3}, you’d call s.clear() to remove all elements of the set. We use the term “this set” to refer to the set on which the method is executed.