Check Two Sets are Equal in Python

Spread the love

How to check set equality in Python (check whether two sets are equal or not)? You can use the == operator to test if two sets are equal in Python. The sets are considered equal if they have the same elements, regardless of the order in which the elements appear. Python Set equality comparison considers if the two sets share the exact same elements, regardless of order.

1. Quick Examples of Equality of Sets

Following are quick examples of how to check the equality of sets in Python.


# Quick Examples

# Check sets equal using ==
print("Set1 and Set2 equal? ",myset1 == myset2)

# Check sets equal using !=
print("Set1 and Set3 Not equal? ",myset1 != myset3)

# Using symmetric_difference()
if (len(myset1.symmetric_difference(myset2))==0):
  print("set1 and set2 are Equal")
else:
  print("set1 and set2 are Not equal")

# Using difference()
if (len(myset1.difference(myset3))==0):
  print("set1 and set3 are Equal")
else:
  print("set1 and set3 are Not equal")

2. Check Sets Equal using == operator

The == operator is used to check whether the two sets are equal or not in Python. If the same elements are present in the two sets then they are considered equal and True is returned, otherwise False is returned. The order of the element doesn’t matter in the equality check.

You can also use the == operator to compare a set to any other iterable object. If the iterable object contains the same elements as the set. Let’s create three sets and check if these are equal or not by using this operator.


# Create three sets
myset1 = {12,90,43,56}
myset2 = {43,56,12,90}
myset3 = {43,56,12}
print("Set 1: ",myset1)
print("Set 2: ",myset2)
print("Set 3: ",myset3)

# Check sets equality using == operator
print("Set1 and Set2 equal? ",myset1 == myset2)
print("Set1 and Set3 equal? ",myset1 == myset3)

# Output:
# Set 1:  {43, 12, 56, 90}
# Set 2:  {43, 12, 56, 90}
# Set 3:  {56, 43, 12}
# Set1 and Set2 equal?  True
# Set1 and Set3 equal?  False

The two sets myset1 and myset2 are equal. Because all elements are the same. But the myset1 and myset3 sets are not equal.

3. Equality of Sets using != operator

The != operator is used to check whether the two sets are equal or not. If all the elements in the sets are equal, then False is returned, otherwise True is returned. Let’s create three sets and use this to check equality.


# Check sets equality using != operator
print("Set1 and Set2 Not equal? ",myset1 != myset2)
print("Set1 and Set3 Not equal? ",myset1 != myset3)

# Output:
# Set1 and Set2 Not equal?  False
# Set1 and Set3 Not equal?  True

The two sets myset1 and myset2 are equal so False is returned. The myset1 and myset3 sets are not equal so True is returned.

4. Equality of Sets using symmetric_difference()

We can also use symmetric_difference() to cehck whether the sets have common or equal elements or not in Python. It will return the elements which are not common in the given sets. So we can utilize this method.

First, we will return the elements that are not common in the given sets. After that, we will find the total number of elements using len() function and check if the length is 0 or not. If the length is 0, then we can say that both sets are equal, Otherwise, they are not equal. We can pass this condition inside the if block.

4.1 symmetric_difference() Syntax


# Syntax
len(myset1.symmetric_difference(myset2))==0

Here,

  • myset1 is the first set and
  • myset2 is the second set

4.2 Check Two Sets are Equal

Let’s use the same sets that are created above and check if the two sets are equal by using symmetric_difference()


# Using symmetric_difference()
if (len(myset1.symmetric_difference(myset2))==0):
  print("set1 and set2 are Equal")
else:
  print("set1 and set2 are Not equal")

# Using symmetric_difference()
if (len(myset1.symmetric_difference(myset3))==0):
  print("set1 and set3 are Equal")
else:
  print("set1 and set3 are Not equal")

# Output:
# set1 and set2 are Equal
# set1 and set3 are Not equal

As per the output, the first two sets are the same (if the block is executed), But the first and third sets are not the same (else block is executed).

5. Check Two Sets Equal using difference()

The set.difference() will return the elements in the first set that are not existing in any of the given sets. We can use this to check the equality of the sets in Python

First, we will return the elements that do not exist in the other sets. After that, we will find the total number of elements using len() function and check if the length is 0 or not. If the length is 0, then we can say that both sets are equal, otherwise, they are not equal. We can pass this condition inside the if block.


# Using difference()
if (len(myset1.difference(myset2))==0):
  print("set1 and set2 are Equal")
else:
  print("set1 and set2 are Not equal")

# Using difference()
if (len(myset1.difference(myset3))==0):
  print("set1 and set3 are Equal")
else:
  print("set1 and set3 are Not equal")

# Output:
# set1 and set2 are Equal
# set1 and set3 are Not equal

As per the output, the first two sets are the same (if the block is executed), But the first and third sets are not the same (else block is executed).

6. Conclusion

In this article, you have learned how to check the equality of sets (check whether two sets are equal or not) in Python. We discussed 4 ways to check the equality of sets. Note that the == operator compares the values of the sets and not the sets objects. Therefore, two sets with the same elements but different memory locations are considered equal.

Leave a Reply

You are currently viewing Check Two Sets are Equal in Python