set.difference_update()

set.difference_update(*others)
Returns: None · Updated March 15, 2026 · Set Methods
sets methods mutation set-operations

The .difference_update() method removes all elements from a set that are present in one or more other sets. Unlike .difference(), which returns a new set, this method modifies the original set directly. The operation is equivalent to set -= other1 | other2 | ....

Syntax

set.difference_update(*others)

Parameters

ParameterTypeDescription
*otherssetOne or more iterable objects (sets, lists, tuples) whose elements will be removed from the original set.

Examples

Basic usage

Remove all elements from one set that exist in another:

a = {1, 2, 3, 4, 5}
b = {3, 4, 5}
a.difference_update(b)
print(a)
# {1, 2}

Using multiple sets

You can remove elements from multiple sets at once:

a = {1, 2, 3, 4, 5, 6, 7}
b = {2, 4, 6}
c = {3, 5, 7}
a.difference_update(b, c)
print(a)
# {1}

This is equivalent to a -= b | c.

With different iterable types

The method accepts any iterable, not just sets:

a = {"apple", "banana", "cherry", "date"}
b = ["banana", "cherry"]  # list
a.difference_update(b)
print(a)
# {apple, date}

Empty result

When all elements are removed, the set becomes empty:

a = {1, 2, 3}
b = {1, 2, 3, 4, 5}
a.difference_update(b)
print(a)
# set()

Using with the -= operator

The -= operator provides a more readable alternative:

a = {1, 2, 3, 4}
b = {3, 4, 5}
a -= b  # equivalent to a.difference_update(b)
print(a)
# {1, 2}

Common Patterns

Filtering a set against a blocklist

A common use case is filtering items against a blocklist:

allowed = {"read", "write", "execute"}
user_permissions = {"read", "write", "delete", "admin"}
allowed.difference_update({"admin"})
print(allowed)
# {read, write, execute}

user_permissions.difference_update(allowed)
print(user_permissions)
# {delete, admin}

Removing common elements from two collections

Find unique elements in each collection:

list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7]

set1 = set(list1)
set2 = set(list2)

unique_to_1 = set(list1)
unique_to_1.difference_update(set2)

unique_to_2 = set(list2)
unique_to_2.difference_update(set1)

print(unique_to_1)  # {1, 2, 3}
print(unique_to_2)  # {6, 7}

In-place set difference with many sets

When working with multiple sets:

def filter_tags(all_tags, *blocked_tag_sets):
    """Remove blocked tags from a set of all tags."""
    all_tags.difference_update(*blocked_tag_sets)
    return all_tags

tags = {"python", "javascript", "rust", "go", "web", "backend", "frontend"}
blocked = {"javascript", "frontend"}
deprecated = {"go"}

result = filter_tags(tags.copy(), blocked, deprecated)
print(result)
# {python, rust, web, backend}

Performance

The .difference_update() method:

  • Time complexity: O(len(s) + len(others))
  • Space complexity: O(len(others)) for creating the union of other sets

This is generally more memory-efficient than creating a new set with .difference() since it modifies in place.

See Also