Python set difference() Tutorial [Practical Examples]

 

Introduction to Python set difference()

The difference between the two sets in Python is equal to the difference between the number of elements in two sets. The Python set difference function returns a set that is the difference between two sets. In this tutorial, we will learn about the python set difference method. We will discuss what is python set is and how we can declare the Python set difference function using various examples.

We will also cover how we can pass multiple arguments to the Python set difference function. At the same time, we will also compare the set difference method with the minus and set difference method with the set_update() method by taking different examples. Moreover, we will also learn about the time complexity of the python set difference method and will touch on other methods that are associated with the Python set.

Advertisement

 

Getting started with the set difference()

Formally A − B = {s | s ∈ A and s ∉ B}. The set difference is a generalization of the idea of the complement of a set and as such is sometimes called the relative complement of B with respect to A. See the diagram below which shows the set difference in pictorial form.

Python set difference

 

Notice that A difference of B contains all the elements of A that are not in B. If two sets ( let say A and B ) are disjoint, then A - B will return the set A because there are no elements of set B present.

 

What is a Python set?

A Python set is an unordered collection of items. Every set element is unique which means there are no duplicate elements in a set and the Python set is immutable (cannot be changed). The curly brackets are used to declare a set in Python. See the syntax of Python set declaration below:

my_set = {element1, element2, ..., elementn}

A set can also be empty as well. Now let us take an example and see how we can declare and print a set in Python, See the example below:

# creating a Python set
my_set = {1, 2, 3, 4, 5, 6, 7}
# printing the type
print(type(my_set))
# printing set
print(my_set)

Output:

<class 'set'>
{1, 2, 3, 4, 5, 6, 7}

Notice that the type is set, which means it will contain all the properties of a set. Let us now try to have duplicate elements in a set. See the example below:

Advertisement
# creating a Python set with duplicate elements
my_set = {1, 2, 2, 2, 2, 2, 2,2, 3, 4, 5, 6, 7}
# printing set
print(my_set)

Output:

{1, 2, 3, 4, 5, 6, 7}

Notice that all the duplicate elements have been removed, because a set cannot contain duplicate elements.

 

Syntax of Python set difference

Now we have enough knowledge about Python sets. Let us know move toward our main goal, and learn how we can use the Python set difference method. Below is the simple syntax of the Python set difference method.

set.difference(*other_set)

The set.difference() method returns the new set with the unique elements that are not in the other set passed as a parameter. The resulting set can have at most as many elements as the given set. This method can take more than one set as arguments separated by commas.

 

Examples of Python set difference

Now let us take an example and see how the python set difference actually works. Let say we have two intersecting sets, setA and setB and we want to find the difference. See the example below:

setA = {1, 2, 3, 4, 5}
setB = {4, 5, 6, 7, 8}
# Python set differene method 
result = setA.difference(setB)
# printing
print(result)

Output:

{1, 2, 3}

Notice that the difference method returns all the unique elements that were not in the second set. If we will try to find the difference between the two disjoint sets, it will return the first set as it is. See the example below:

# creating a Python sets
setA = {1, 2, 3, 4, 5}
setB = { 6, 7, 8, 9, 0}
# Python set differene method 
result = setA.difference(setB)
# printing
print(result)

Output:

Advertisement
{1, 2, 3, 4, 5}

So the difference return the same set because there were no elements of the second set. If we will find the difference of set with an empty set, again it will return the same set. See the example below:

# creating a Python sets
setA = {1, 2, 3, 4, 5}
setB = { }
# Python set differene method 
result = setA.difference(setB)
# printing
print(result)

Output:

{1, 2, 3, 4, 5}

Notice that we get the same set because we are finding the difference with an empty set. If we will find the difference with its own, then we will get an empty set. See the example below:

# creating a Python sets
setA = {1, 2, 3, 4, 5}
# Python set differene method 
result = setA.difference(setA)
# printing
print(result)

Output:

set()

We get an empty set because we subtract the set from itself which returns an empty set.

 

Python set difference with multiple arguments

So far we have used only one argument while calling the set difference method. It can take multiple arguments and will return all the unique elements that are not found in any of the provided arguments. See the example below which call the set difference method by passing multiple arguments.

# creating a Python sets
setA = {1, 2, 3, 4, 5}
setB = {3, 4, 5, 6, 7}
setC = {2, 3, 4, 9, 10}
# Python set differene method 
result = setA.difference(setB, setC)
# printing
print(result)

Output:

{1}

We get only one element because it is the only unique element that is not found in any of the provided arguments. The pictorial representation of the set difference method with multiple arguments looks like this: See the diagram below:

Advertisement

Python multiple arguments

The set difference will return all the unique elements from the set, that are not found in any of the arguments ( provided sets).

 

Python set difference vs Minus

Apart from the set difference method, we can also use the minus sign to find the set difference. All we need to do is to put a sign symbol between the sets. See the example below which find the set difference using the minus sign.

# creating a Python sets
setA = {1, 2, 3, 4, 5}
setB = {3, 4, 5, 6, 7}
# Python set differene method 
result1 = setA.difference(setB)
result2 = setA - setB
# printing
print("Using set difference method; ",result1)
print("Using the minus symbol : ", result2) 

Output:

Using set difference method; {1, 2}
Using the minus symbol : {1, 2}

Notice that we get the same result because the minus symbol will remove all the elements that are in both the sets and will return only the unique elements from setA that are not found in setB. We can also use this method for multiple sets as well. See the example below:

# creating a Python sets
setA = {1, 2, 3, 4, 5}
setB = {3, 4, 5, 6, 7}
SetC = {2, 3, 6, 7, 8}
# Python set differene method 
result1 = setA.difference(setB, SetC)
result2 = setA - setB - SetC
# printing
print("Using set difference method; ",result1)
print("Using the minus symbol : ", result2) 

Output:

Using set difference method; {1}
Using the minus symbol : {1}

Again we get the same result because the minus sign works in a similar way as the set difference method does.

 

Python set difference vs difference_update() method

The set difference method helps to find out the difference between sets and returns a new set with the difference value, but the difference_update() updates the existing caller set. For example, if setA and setB are two sets. The set difference method will return a new set of different values. While the set difference_update() method modifies the existing set. If (setA – setB) is performed, then setA gets modified into (setA – setB). See the example below:

# creating a Python sets
setA = {1, 2, 3, 4, 5}
setB = {3, 4, 5, 6, 7}
# Python set differene method 
setA.difference(setB)
# printing
print("SetA after applying set difference method: ", setA)
# Python set difference update method
setA.difference_update(setB)
# printing
print("SetA after applying set difference update method: ", setA)

Output:

SetA after applying set difference method: {1, 2, 3, 4, 5}
SetA after applying set difference update method: {1, 2}

Notice that the set difference method does not modify the original set, while the set difference_update() method removes all the elements from the original set except the uniques ones.

 

The time complexity of the Python set difference

Time complexity is the amount of time taken by an algorithm to run, as a function of the length of the input. It measures the time taken to execute each statement of code in an algorithm. The runtime complexity of the python set difference method on a set with n elements and a set argument with m elements is O(n) because you need to check for each element in the first set whether it is a member of the second set. Checking membership is O(1), so the runtime complexity is O(n) * O(1) = O(n).

 

More Python set methods

There are various different kinds of set methods available that can be used to perform different operations on sets. Some of which are listed below along with the operations that they performed on sets. See the list below:

  • set.add() : Used to add the elements to set.
  • set.clear() : Removes all the elements from the set.
  • set.copy() : Create and return a flat copy of this set.
  • set.discard() : Remove an element from this set if it is a member, otherwise do nothing.
  • set.intersection() : Create and return a new set that contains all elements that are members of all sets.
  • set.pop() : Remove and return a random element from this set. If the set is empty, it’ll raise a KeyError.
  • set.remove() : Remove and return a specific element from this set as defined in the argument.

 

Summary

A Set is an unordered collection data type that is iterable, mutable, and has no duplicate elements. In Python, there are various methods available that are used to perform different operations on sets. One of them about which we learned in this tutorial was the set difference method. We learned about the syntax of the python set difference method along with solving various examples.

We also discussed the python set difference method with multiple arguments as well. At the same time, we came across another way to find the difference of sets apart from using the set difference method. Moreover, we covered the time complexity of the set difference method as well. To summarize, this tutorial contains all the necessary information that you need to know in order to start working with Python set difference method.

 

Further Reading

Pythons sets
Python set difference
Python set methods

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment