Skip to Content

How To Find The Difference Between Two Or More Sets In Python

How can you find the difference between two sets in Python?

In Python, you can find the difference between two sets using the .difference() method. The set and frozenset objects contain a built-in method labelled difference() which helps to find the non-matching elements in the source object not found in the other sets passed in to the .difference(*others) parameter.

Here is an example demonstrating how to find the difference between two sets using the Python REPL:

If and Else Statements in Python (V...
If and Else Statements in Python (Video 13)
>>> source_set = {10, 12, 14, 16, 18}
>>> other_set = {12, 16, 18}
>>> source_set.difference(other_set)
{10, 14}

As you can see from the above output the source_set is used as the baseline, and any corresponding elements that are found in the other_set are removed, leaving the result of {10, 14}.

Difference Between Multiple Sets

You can add more sets into the .difference() parameters if there are other sets to compare against.

For example, you could compare three sets and find which values remain in the source_set after comparing them against two other sets, as demonstrated below:

>>> source_set = {10, 12, 14, 16, 18}
>>> other_set = {12, 16, 18}
>>> third_set = {10}
>>> source_set.difference(other_set, third_set)
{14}

As you can see the source_set is compared against each of the sets in the difference() method. The result can never be more than what is in the original source_set.

What if you just wanted to update the source_set rather than produce a new set?

Remove Differences By Updating Source Set

What if you didn’t want to produce a new set but rather wanted to just update the source_set?

There is another method called .difference_update(*others) which enables this functionality.

Using the previous example above, here’s how this would look in the Python REPL:

>>> source_set = {10, 12, 14, 16, 18}
>>> other_set = {12, 14, 16}
>>> source_set.difference_update(other_set)
>>> source_set
{10, 14}

As you can see the method performs the necessary task of removing the shared elements from the source_set. If there are multiple sets to compare, you could wrap them all into one parameter as previously done with the .difference(*others) method.

Here’s how this would look applying multiple sets on the source_set:

>>> source_set = {10, 12, 14, 16, 18}
>>> other_set = {12, 16, 18}
>>> third_set = {10}
>>> source_set.difference_update(other_set, third_set)
>>> source_set
{14}

As you can see, this produces the same outcome and mutates the original source_set variable.

Summary

In Python, you can find the difference between two sets using the .difference() method. The set and frozenset objects contain a built-in method labelled difference(*others) which helps to find the non-matching elements in the source object not found in the other sets passed in to the *others parameter. You can add more sets into the .difference() method if there are other sets to compare against.

If you would prefer to update the original source_set being compared against rather than producing a new set, you could use another method called .difference_update(*others).