Python’s `set.difference_update(*args)`

method removes all elements from this set that are members of any of the given set arguments. For example, `s.difference_update({1, 2})`

removes elements 1 and 2 from the set `s`

. Its return value is `None`

because it modifies the set it is called upon rather than creating a new set.

Here’s a minimal example where we remove elements 1 and 2 from an existing set:

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

Table of Contents

## Syntax

Let’s dive into the formal syntax of the `set.difference_update()`

method.

set.difference_update(*sets)

Argument | Data Type | Explanation |
---|---|---|

`*sets` | One or more sets | The elements of those sets will be removed from the existing set. |

For comprehensibility, here’s the output of the `help()`

function:

>>> help(set.difference_update) Help on method_descriptor: difference_update(...) Remove all elements of another set from this set.

## Return Value

The return value of `set.difference_update()`

is None but the method has side effects: it removes zero or more elements from the set it is called upon—depending on the sets passed as method arguments.

## Examples

There are some subtleties you need to understand regarding the set `difference_update()`

method. Let’s dive into them by example!

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

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

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

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

Can you compute the difference between a set and an empty set? Sure! The original set doesn’t change!

>>> s = {1, 2, 3} >>> s.difference_update(set()) >>> s {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.

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

## Multiple Set Arguments

You can compute the difference between an original set and an arbitrary number of set arguments. In this case, the original set will be modified so that it contains only elements that are not members of * any *of the multiple set arguments.

Here’s an example:

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

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

## 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_update()?

The runtime complexity of the `set.difference_update()`

function on a set with ** n** elements and a set argument with

**elements is**

*m***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(n)***, so the runtime complexity is**

*O(1)***. In fact, if the second set is smaller, the runtime complexity is smaller as well, i.e.,**

*O(n) * O(1) = O(n)**–> set difference is*

**m<n****.**

*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

**(8th Gen) processor and 16GB of memory. Here’s the code of the experiment:**

*Intel Core i7*import matplotlib.pyplot as plt 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_update(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.

`add()` | Add an element to this set |

`clear()` | Remove all elements from this set |

`copy()` | Create and return a flat copy of this set |

`difference()` | Create and return a new set containing all elements of this set except the ones in the given set arguments. The resulting set has at most as many elements as any other. |

`difference_update()` | Remove all elements from this set that are members of any of the given set arguments. |

`discard()` | Remove an element from this set if it is a member, otherwise do nothing. |

`intersection()` | Create and return a new set that contains all elements that are members of all sets: this and the specified as well. . |

`intersection_update()` | Removes all elements from this set that are not members in all other specified sets. |

`isdisjoint()` | Return `True` if no element from this set is a member of any other specified set. Sets are disjoint if and only if their intersection is the empty set. |

`issubset()` | Return `True` if all elements of this set are members of the specified set argument. |

`issuperset()` | Return `True` if all elements of the specified set argument are members of this set. |

`pop()` | Remove and return a random element from this set. If the set is empty, it’ll raise a `KeyError` . |

`remove()` | Remove and return a specific element from this set as defined in the argument. If the set doesn’t contain the element, it’ll raise a `KeyError` . |

`symmetric_difference()` | Return a new set with elements in either this set or the specified set argument, but not elements that are members of both. |

`symmetric_difference_update()` | Replace this set with the symmetric difference, i.e., elements in either this set or the specified set argument, but not elements that are members of both. |

`union()` | Create and return a new set with all elements that are in this set, or in any of the specified set arguments. |

`update()` | Update this set with all elements that are in this set, or in any of the specified set arguments. The resulting set has at least as many elements as any other. |

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.