Python Set difference()

Video Tutorial
FREE
Difference thumbnail
This video belongs to
Python Course for Beginners With Certification: Mastering the Essentials
16 modules
Certificate
Topics Covered

Overview

The set difference in python can be calculated using the python set difference() method and python set difference operator, i.e. -. Both the python set difference method and operator return a set containing elements that exist only in the first set and not in both the sets.

Syntax of python Set difference()

The syntax of the python set difference() method is quite simple. The set difference method takes the comparing set as a parameter.

set_one elements will be compared with the set_two elements.

The elements present in the set_one and not in set_two will be the output set.

Note: The syntax of the python set difference operator is also very simple:

Parameters of Python Set difference()

As discussed above, the python set difference() method takes another set as a parameter. The parameter set will be compared with the function caller set. After the comparison, a new set will be generated and returned by the python set difference method.

Return Values of Python Set difference()

Using the set passed as a parameter to the python set difference() method, the method will generate a new set and returns the generated set.

The returned set may be empty if both input sets contain the same elements.

Exceptions of Python Set difference()

Usually, the python set difference() method does not raise an error if we use the correct syntax.

The difference() function returns a set, which has the set difference of the two input sets. If we do not pass any set in the parameter of the difference() method, the difference() method returns a copy of the calling set itself, i.e. set_one.

Example of Python Set difference()

Let us take the example of two sets, namely set_1, and set_2. We will try to find their difference using the python set difference() method and python set difference operator.

Output:

As we can see, the first set contains a, b, c and the second set contains g, m, a. The output or resulting set will contain only m and g because these two elements occur in the second but not in the first set.

The python set difference() method and python set difference operator will generate the same set as the output set.

What is Set difference() in Python?

The difference between two sets is known as set difference. When we do set difference on two sets using the python set difference() method or python set difference operator, a new resulting set is generated. The resulting set contains the elements from the first set which are not present in the second set.

Let us take the Venn diagram to visualize the python set difference.

Python Set Difference Diagram

Suppose we have two sets, namely A and B. The set difference between A and B can be calculated in two ways:

1. (A-B) or A.difference(B):

(A-B) the set difference will result in the elements present in set A but not in set B.

2. (B-A) or B.difference(A):

(B-A) the set difference will result in the elements present in set B but not in set A.

Note: If the two equal set or the elements of the first set is completely present in the other, the resulting set will be empty.

The Python Set difference() method vs Python Set difference operator (-)

The python set difference() method and - operator have the same performance. The only difference between the operator and method is that the python set difference operator only works with sets. In contrast, the python set difference() method can be used with any iterable, like strings, lists, and dictionaries. The set difference operator can compare a set with other iterables as well.

More Example

Let us take the example of two iterables i.e. one set and one list containing some numbers, and try to get their difference.

Using Python Set difference() Method

Output:

When the python set difference() method compares the elements of the set with the list, the elements present in the set but not in the list are returned as a new set.

Using Python Set difference() Operator

Output:

When the set difference operator tried to compare the set with the list, an error was generated because the python set difference operator can only work with sets, not lists.

Conclusion

  • The Python set difference() method and python set difference operator i.e. - is used to get the difference of two sets.
  • The syntax of Python set difference() method is:
  • The Python set difference() method takes the other comparable set as a parameter.
  • The Python set difference() method returns a set consisting of the elements present in the set_one but not in the set_two. The Python set difference() method can be used with any iterable, like strings, lists, and dictionaries.
  • The Python set difference() operator only works with sets. The syntax of - or python set difference operator is:

Read More: