Last modified: Mar 17, 2026 By Alexander Williams

Python Set Operations Guide: Union, Intersection, Difference

Python sets are powerful. They store unique, unordered items. Set operations let you compare and combine these collections. This guide explains the core operations.

You will learn union, intersection, and difference. We cover symmetric difference and membership tests too. Each concept includes simple code examples.

What Are Python Sets?

A set is a built-in data type. It holds an unordered collection of unique items. Items must be immutable, like numbers or strings.

You create a set with curly braces {} or the set() function. Duplicates are automatically removed. This makes sets ideal for finding distinct values.

For a deeper dive into set fundamentals, see our Python Sets Guide: Unordered Unique Collections.


# Creating sets
fruits = {"apple", "banana", "cherry"}
numbers = set([1, 2, 3, 3, 2])  # Duplicates removed

print(fruits)
print(numbers)
    

{'cherry', 'banana', 'apple'}
{1, 2, 3}
    

Core Set Operations

Set operations are methods or operators. They perform comparisons between sets. The main operations are union, intersection, and difference.

You can use methods like .union() or operators like |. Both give the same result. Operators often provide cleaner syntax.

Union Operation

The union combines all items from multiple sets. It returns a new set with elements from all sets. Duplicates are, of course, not included.

Use the union() method or the | operator. For more on adding items, check Python Set Insert: How to Add Items.


set_a = {1, 2, 3}
set_b = {3, 4, 5}

# Using the union() method
union_result = set_a.union(set_b)
print("Union (method):", union_result)

# Using the | operator
union_operator = set_a | set_b
print("Union (operator):", union_operator)
    

Union (method): {1, 2, 3, 4, 5}
Union (operator): {1, 2, 3, 4, 5}
    

Intersection Operation

The intersection finds common items. It returns a set containing only elements present in all compared sets.

Use the intersection() method or the & operator. This is perfect for finding overlaps.


set_x = {"a", "b", "c", "d"}
set_y = {"c", "d", "e", "f"}

# Using the intersection() method
inter_result = set_x.intersection(set_y)
print("Intersection (method):", inter_result)

# Using the & operator
inter_operator = set_x & set_y
print("Intersection (operator):", inter_operator)
    

Intersection (method): {'c', 'd'}
Intersection (operator): {'c', 'd'}
    

Difference Operation

The difference finds items in one set but not in another. It returns a new set with elements unique to the first set.

Use the difference() method or the - operator. The order of sets matters here.


set_1 = {10, 20, 30, 40}
set_2 = {30, 40, 50, 60}

# Using the difference() method
diff_result = set_1.difference(set_2)
print("Difference (method):", diff_result)

# Using the - operator
diff_operator = set_1 - set_2
print("Difference (operator):", diff_operator)

# Order changes the result
print("Reverse difference:", set_2 - set_1)
    

Difference (method): {10, 20}
Difference (operator): {10, 20}
Reverse difference: {50, 60}
    

Other Useful Set Operations

Beyond the core three, Python offers more. These include symmetric difference and subset checks.

Symmetric Difference

Symmetric difference finds items in either set, but not in both. It's like an exclusive OR (XOR) operation.

Use the symmetric_difference() method or the ^ operator.


set_p = {"red", "green", "blue"}
set_q = {"blue", "yellow", "cyan"}

# Using symmetric_difference()
sym_result = set_p.symmetric_difference(set_q)
print("Symmetric Difference (method):", sym_result)

# Using the ^ operator
sym_operator = set_p ^ set_q
print("Symmetric Difference (operator):", sym_operator)
    

Symmetric Difference (method): {'cyan', 'green', 'yellow', 'red'}
Symmetric Difference (operator): {'cyan', 'green', 'yellow', 'red'}
    

Membership and Comparison Tests

You can test if an item is in a set with in. You can also check subset and superset relationships.

Use issubset() or <=. Use issuperset() or >=. These are very efficient.


primary = {"red", "green", "blue"}
colors = {"red", "green", "blue", "yellow"}

# Membership test
print("Is 'red' in primary?", "red" in primary)

# Subset test
print("Is primary a subset of colors?", primary.issubset(colors))
print("Using operator:", primary <= colors)

# Superset test
print("Is colors a superset of primary?", colors.issuperset(primary))
print("Using operator:", colors >= primary)
    

Is 'red' in primary? True
Is primary a subset of colors? True
Using operator: True
Is colors a superset of primary? True
Using operator: True
    

Practical Applications and Examples

Set operations solve real problems. They are great for data cleaning and analysis.

Use them to find unique visitors, common tags, or missing data. For more complex structures, see Python Set of Sets: Nested Collections Guide.


# Example: Finding common interests between two users
user_a_interests = {"python", "data science", "cycling", "reading"}
user_b_interests = {"python", "hiking", "reading", "gaming"}

common_interests = user_a_interests & user_b_interests
print("Common Interests:", common_interests)

# Example: Removing stopwords from text
all_words = {"the", "quick", "brown", "fox", "the", "jumps"}
stopwords = {"the", "a", "an", "and"}

filtered_words = all_words - stopwords
print("Words after removing stopwords:", filtered_words)
    

Common Interests: {'python', 'reading'}
Words after removing stopwords: {'brown', 'jumps', 'quick', 'fox'}
    

Conclusion

Python set operations are essential tools. They provide a fast way to manage unique collections.

You learned union, intersection, and difference. We also covered symmetric difference and membership tests.

Remember: Sets are unordered and only store unique items. Operations like union() and intersection() are highly optimized.

Use these operations to write cleaner, more efficient code. They are perfect for deduplication, comparison, and filtering tasks. For more patterns, explore our Python Sets Examples: Code for Unique Data.