Skip to main content

How to Combine Two or More Sets in Python

Python's set is a powerful, unordered data structure that stores unique elements. A common task is to combine multiple sets to find their union (all unique elements), intersection (common elements), or difference (unique elements in one set but not another). Python provides intuitive methods and corresponding operators to perform these operations efficiently.

This guide will walk you through the primary ways to combine sets based on the logical outcome you want to achieve, distinguishing between methods that create a new set and those that modify a set in-place.

Combining All Unique Items (Union)

A union combines all elements from multiple sets into a new set. Since sets only store unique items, duplicates are automatically removed.

Using the union() Method or | Operator

Both the .union() method and the | operator create and return a new set containing all unique elements from the sets being combined. The original sets are not modified.

Solution:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {5, 6, 7}

# Using the .union() method
union_set_method = set1.union(set2, set3)
print(f"Result from .union(): {union_set_method}")

# Using the | operator (more concise)
union_set_operator = set1 | set2 | set3
print(f"Result from | operator: {union_set_operator}")

Output:

Result from .union(): {1, 2, 3, 4, 5, 6, 7}
Result from | operator: {1, 2, 3, 4, 5, 6, 7}
note

The | operator is often preferred for its brevity and readability, especially when combining multiple sets.

Using the update() Method for In-Place Union

The .update() method (or its operator version |=) also performs a union, but it modifies the original set in-place instead of creating a new one. It adds all elements from other sets into the set the method is called on.

Solution:

set1 = {1, 2, 3}
set2 = {3, 4, 5}

# The contents of set2 are added to set1
set1.update(set2)

print(f"set1 after update: {set1}")

Output:

set1 after update: {1, 2, 3, 4, 5}

Finding Only Common Items (Intersection)

An intersection creates a new set containing only the elements that are present in all of the specified sets.

Solution:

set1 = {'apple', 'banana', 'cherry'}
set2 = {'banana', 'cherry', 'date'}
set3 = {'cherry', 'fig', 'grape'}

# Using the .intersection() method
intersection_set_method = set1.intersection(set2, set3)
print(f"Result from .intersection(): {intersection_set_method}")

# Using the & operator
intersection_set_operator = set1 & set2 & set3
print(f"Result from & operator: {intersection_set_operator}")

Output:

Result from .intersection(): {'cherry'}
Result from & operator: {'cherry'}

The element 'cherry' is the only one present in all three sets.

Finding a Set's Unique Items (Difference)

A difference finds elements that are in one set but not in others. There are two types of difference operations.

Asymmetric Difference (difference() or - Operator)

This operation returns a new set with elements that are in the first set but not in the second set. The order matters.

Solution:

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

# Elements in set1 but not in set2
diff_set = set1.difference(set2)
# Using the - operator
diff_set_operator = set1 - set2

print(f"Result from .difference(): {diff_set}")
print(f"Result from - operator: {diff_set_operator}")

Output:

Result from .difference(): {1, 2}
Result from - operator: {1, 2}

Symmetric Difference (symmetric_difference() or ^ Operator)

This operation returns a new set with all elements that are in exactly one of the sets—that is, elements that are not shared between them.

Solution:

set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}

# Elements that are in either set1 or set2, but not both
sym_diff_set = set1.symmetric_difference(set2)
# Using the ^ operator
sym_diff_set_operator = set1 ^ set2

print(f"Result from .symmetric_difference(): {sym_diff_set}")
print(f"Result from ^ operator: {sym_diff_set_operator}")

Output:

Result from .symmetric_difference(): {1, 2, 5, 6}
Result from ^ operator: {1, 2, 5, 6}

Here, 3 and 4 are excluded because they are present in both sets.

Conclusion

Python provides a rich and intuitive toolkit for combining sets, and the best method depends on the desired outcome:

  • To get all unique items from all sets (Union): Use the .union() method or the | operator. To modify a set in-place, use .update().
  • To get only the items common to all sets (Intersection): Use the .intersection() method or the & operator.
  • To get items in one set but not another (Difference): Use the .difference() method or the - operator.
  • To get items that are in either set but not both (Symmetric Difference): Use the .symmetric_difference() method or the ^ operator.

For creating new sets, using the concise operators (|, &, -, ^) is often the most Pythonic and readable choice.