How to combine two or more sets in Python

Depending on the result you want to get, are three easy ways to combine two or more sets in Python:

  1. Using the union() method or operator
  2. Using the update() method
  3. Using the intersection() method
  4. Using the difference() method

This tutorial will show you how to use the three solutions above in practice.

1. Use the union() method or operator

The union() method in the Set object can be used to combine two or more sets into one.

Any duplicate items in the sets are included only once in the new set. Here’s an example of calling the method:

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

new_set = set1.union(set2)
print(new_set)  # {1, 2, 3, 4, 5}

If you have more than two sets, you can chain the .union() call and pass the next set object as follows:

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

new_set = set1.union(set2).union(set3)

print(new_set)  # {1, 2, 3, 4, 5, 7, 8}

You can also shorten the code by using the union operator | to join the sets as follows:

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

new_set = set1 | set2 | set3

print(new_set)  # {1, 2, 3, 4, 5, 7, 8}

You might prefer using the union operator when you have many sets to combine. It makes the code cleaner.

2. Use the update() method

The update() method is used to combine two or more sets like the union() method. The difference is that the update() method modifies the original set instead of returning a new one.

Here’s an example of the update() method in action:

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

set1.update(set2)

print(set1)  # {1, 2, 3, 4, 5}

The update() method mutates the set1 object and returns None.

3. Use the intersection() method

The intersection() method is used to create a new set that combines the values present in two or more sets.

When you combine two sets, any values that don’t exist in both sets will be excluded:

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

new_set = set1.intersection(set2)

print(new_set)  # {2, 3}

The new_set object in the example above only has values that exist in set1 and set2.

4. Use the difference() method

The difference() method is used to create a new set object that has values unique to the set you call this method from.

Here’s an example of using the difference() method:

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

new_set = set1.difference(set2)

print(new_set)  # {1, 2}

The number 3 is excluded because it exists in set2. If you want to also include all values unique to set2, you can use the symmetric_difference() method instead.

This method includes any values unique to the set2 as follows:

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

new_set = set1.symmetric_difference(set2)

print(new_set)  # {1, 2, 4, 5}

This time, only the number 3 is excluded from the new_set object.

Conclusion

Depending on the result you want to achieve, there are four ways you can combine two or more sets as one in Python.

To combine all unique values from the sets, use the union() method.

To combine values present in the sets, use the intersection() method.

To combine values unique to the sets, use the difference() or symmetric_difference() method.

Now you’ve learned how to combine sets in Python. Nice work! 🙌

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.