set.difference()

set.difference(*others)
Returns: set · Updated March 15, 2026 · Set Methods
sets methods immutable-operation

The .difference() method returns a new set containing all elements that are in the calling set but not in the provided iterables. This is useful for finding what is unique to one set compared to others. The operation does not modify the original set.

Syntax

set.difference(*others)

Parameters

ParameterTypeDefaultDescription
*othersiterableOne or more iterables to compare against. Elements in these iterables will be excluded from the result.

Examples

Basic usage

Find elements in one set but not in another:

a = {1, 2, 3, 4}
b = {3, 4, 5, 6}

result = a.difference(b)
print(result)
# {1, 2}

Multiple iterables

You can compare against multiple iterables at once:

a = {1, 2, 3, 4, 5}
b = {2, 4}
c = {4, 5, 6}

result = a.difference(b, c)
print(result)
# {1, 3}

Using with different types

The method works with any iterable, not just sets:

numbers = {1, 2, 3, 4, 5}
exclusions = [3, 4]  # List works too

result = numbers.difference(exclusions)
print(result)
# {1, 2, 5}

Finding unique elements across multiple sources

A practical use case is finding items unique to one collection:

users_2023 = {"alice", "bob", "charlie", "david"}
users_2024 = {"alice", "bob", "eve", "frank"}

new_users = users_2024.difference(users_2023)
print(new_users)
# {eve, frank}

churned_users = users_2023.difference(users_2024)
print(churned_users)
# {charlie, david}

Working with strings

Strings are iterable, so you can use them with .difference():

vowels = set("aeiou")

# Find letters in hello that are not vowels
letters_in_hello = set("hello")
non_vowels = letters_in_hello.difference(vowels)
print(non_vowels)
# {h, l}

This is useful for character analysis in text processing.

difference() vs difference_update()

Understanding when to use which:

original = {1, 2, 3, 4}
others = {3, 4}

# .difference() returns a NEW set, original unchanged
result = original.difference(others)
print(original)  # {1, 2, 3, 4} — unchanged
print(result)    # {1, 2} — new set

# .difference_update() modifies in place (not shown, but similar logic)

Use .difference() when you need to keep the original data intact. Use .difference_update() when you want to modify the original set directly.

Common Patterns

Tag exclusion

all_tags = {"python", "tutorial", "beginner", "advanced", "code"}
exclude = {"advanced"}

beginner_tags = all_tags.difference(exclude)
print(beginner_tags)
# {python, tutorial, beginner, code}

Permission checking

user_permissions = {"read", "write", "delete", "admin"}
required = {"read", "write", "execute"}
missing = required.difference(user_permissions)

if missing:
    print(f"Missing permissions: {missing}")
else:
    print("All permissions granted")
# Missing permissions: {execute}

Data cleanup

Remove unwanted values from a dataset:

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
invalid = {7, 8, 9, 10}  # Outliers or invalid entries

valid_data = set(data).difference(invalid)
print(valid_data)
# {1, 2, 3, 4, 5, 6}

Performance

The .difference() method has:

  • Time complexity: O(len(s) + sum(len(other) for other in others))
  • Space complexity: O(len(s)) for the result set

This makes it efficient for finding differences between sets, especially when the sets are of similar size.

See Also