Python

Python Set Difference

In Python, a set is an unsorted collection of elements/items. Each set element has to be distinct (no duplicates) and unchangeable.  A set, on the other hand, is malleable (changeable). It is possible to add or delete data items from it. Calculations such as sum, intersection, and symmetric difference can be conducted on sets. A set can be made by enclosing all of the items (elements) in curly brackets, separating them with a comma, and then using the built-in set() method. It can include an unlimited number of elements of various categories (integer, float, string, etc.).

On the other hand, a set cannot contain mutable items such as lists, sets, or dictionaries. Python sets and the four amazing operations they offer Intersection in which elements are shared by two sets. Union in which all of the components from both sets are joined. Elements present on one set but not on the other are performed by difference. And the symmetric difference in which elements from one set are absent from the other. The different operations in Python will be discussed in this post.

The difference in the two sets is equal to the number of items in the two sets. The difference() method gives a set. This set comprises the items that differ from the other two sets. Let’s see if we can figure out the difference between sets A and B. The items in set A that are not in set B will be (set A – set B), whereas those in set B that are not available in set A will be (set B – set A). The syntax is A.difference(B). This is the same as A-B. This technique takes A set as a parameter. It provides a set as a result of the difference between the two others. A.difference(B) returns a set containing the entries from set A but not from set B. This (-) operator could be used for the same reason. One or more iterables (e.g., strings, lists, and dictionaries) can be sent to the set difference() method. But the set difference operator (-) only accepts sets. When you call the set difference() method with iterables, it will convert them to sets before completing the difference operation.

Let’s look at the set difference method in Python with examples.

Example 1:

We have a total of two sets, A and B, in this example. “Python,” “8, “Hello,” and “Guide” are among the elements in A, while “Hello,” “5,” and “Guide” are among the elements in B. The difference() method is used to find the variance between set A and set B and in the same way between B and A.

A = {"Python", 8, 7, "Hello"}

B = {6, "Hello", 5, "Guide"}

print("A-B is:",A.difference(B))

print("B-A is:",B.difference(A))

This is the result. The first line in this example indicates the difference between A and B. The difference between B and A is shown on the second line.

Example 2:

On Sets, we can also make use of the – operator. This method functions similarly to the difference() method. Let’s repeat the previous example, but now this time only with the – operator.

A = {"Python", 8, 7, "Hello"}

B = {6, "Hello", 5, "Guide"}

print("A-B is:",A-B)

print("B-A is:",B-A)

As you can see, the difference is below.

Example 3:

We’ll pass a different number of sets and show the difference between them. We’ve initialized three sets with the names set1, set2, and set3. Set 1 has 2,2,6,2,3,4 elements, whereas set 2 has 5,6,5,6,7,7 components. Set3 has 3,6,7,8,9 components respectively. The difference between them was then determined.

set1 = {2, 2, 6, 2, 3, 4}

set2 = {5, 6, 5, 6, 7, 7}

set3 = {3, 6, 7, 8, 9}

res = set1.difference(set2, set3)

print(res)

See the results in the screenshot below.

Example 4:

The difference_update() technique also returns the difference among set members from iterable objects. This function does not send the result of the difference operation in a newly built set; instead, it updates the same set on which the function was performed. We have two sets named set1 and set2 in this example. In curly brackets, there is a 1,3,5,7,9 item in Set1. Set2 has 2,4,5,7,9 elements, while set1 has 2,4,5,7,9 elements, respectively. We’ve started by printing the original set, which hasn’t been altered in any way. The difference_update method was then used to find the difference, and the result was then updated.

set1 = {1, 3, 5, 7, 9}

set2 = {2, 4, 6, 7, 9}

print("Set before the difference operation: ", set1)

set1.difference_update(set2)

print("set1 difference set2: ", set1);

Here is the output in which you can observe the difference between set1 and set2.

Conclusion:

When there is a difference between two sets, a new set is made up of items in the first set but not in the second. You can provide as many sets as you like; simply use a comma to separate them. The set difference operator in Python can be used to find the difference between two sets equal to the number of elements they have. The difference() function returns a set representing the distinction between different sets. We looked at a number of instances in this post to see how to set components differently in Python.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content