Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program To Check Two Set Are Equal
Python sets are unordered collections of unique elements that are useful for mathematical operations. Checking if two sets are equal is a common task that can be accomplished using several methods.
What Is A Set?
A set is an unordered collection data type that is iterable, mutable and has no duplicate elements. Sets are represented by curly brackets {} and are highly optimized for checking whether a specific element is present.
Sets are created by placing elements inside curly brackets, separated by commas, or by using the built-in set() function.
Example
# Simple set
names = {"ahana", "jacob", "yana", "ankush", "umang"}
print(names)
# Mixed data types
mixed_set = {1, "ahana", 2, "jacob", 3.5, "yana"}
print(mixed_set)
{'yana', 'ahana', 'jacob', 'ankush', 'umang'}
{1, 2, 3.5, 'yana', 'ahana', 'jacob'}
Methods to Check Set Equality
There are four main methods to check if two sets are equal in Python:
Using "==" operator Direct comparison
Using "issubset()" Check if sets are subsets of each other
Using "symmetric_difference()" Check if difference is empty
Using set operations Compare using union and intersection
Method 1: Using "==" Operator
The == operator compares two sets directly. It returns True if both sets contain exactly the same elements, regardless of order.
Example
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3, 4, 5}
set3 = {5, 4, 3, 2, 1} # Same elements, different order
print("set1 == set2:", set1 == set2)
print("set1 == set3:", set1 == set3) # Order doesn't matter
# Different sets
set4 = {1, 2, 3}
print("set1 == set4:", set1 == set4)
set1 == set2: True set1 == set3: True set1 == set4: False
Method 2: Using issubset()
Two sets are equal if each is a subset of the other. The issubset() method returns True if all elements of one set are present in another.
Example
set_a = {1, 2, 3}
set_b = {1, 2, 3, 4, 5}
set_c = {1, 2, 3}
# Check if sets are equal using mutual subset relationship
def are_sets_equal(s1, s2):
return s1.issubset(s2) and s2.issubset(s1)
print("set_a and set_b equal:", are_sets_equal(set_a, set_b))
print("set_a and set_c equal:", are_sets_equal(set_a, set_c))
print("set_a.issubset(set_b):", set_a.issubset(set_b))
print("set_b.issubset(set_a):", set_b.issubset(set_a))
set_a and set_b equal: False set_a and set_c equal: True set_a.issubset(set_b): True set_b.issubset(set_a): False
Method 3: Using symmetric_difference()
The symmetric_difference() method returns elements that are in either set but not in both. If two sets are equal, their symmetric difference will be empty.
Example
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3, 4, 5}
set3 = {1, 2, 3}
def check_equality_symmetric_diff(s1, s2):
return len(s1.symmetric_difference(s2)) == 0
print("set1 and set2 equal:", check_equality_symmetric_diff(set1, set2))
print("set1 and set3 equal:", check_equality_symmetric_diff(set1, set3))
# Show the actual difference
print("Difference between set1 and set3:", set1.symmetric_difference(set3))
set1 and set2 equal: True
set1 and set3 equal: False
Difference between set1 and set3: {4, 5}
Method 4: Using Set Operations
Two sets are equal if their union equals their intersection in terms of elements.
Example
set1 = {1, 2, 3, 4}
set2 = {4, 3, 2, 1}
set3 = {1, 2, 5}
def check_equality_operations(s1, s2):
return s1.union(s2) == s1.intersection(s2).union(s1).union(s2)
# Simpler approach: union should equal both original sets
def sets_equal_simple(s1, s2):
return s1.union(s2) == s1 and s1.union(s2) == s2
print("set1 and set2 equal:", sets_equal_simple(set1, set2))
print("set1 and set3 equal:", sets_equal_simple(set1, set3))
set1 and set2 equal: True set1 and set3 equal: False
Comparison of Methods
| Method | Syntax | Performance | Readability |
|---|---|---|---|
| == operator | set1 == set2 |
Fastest | Most readable |
| issubset() | set1.issubset(set2) and set2.issubset(set1) |
Moderate | Verbose |
| symmetric_difference() | len(set1.symmetric_difference(set2)) == 0 |
Slower | Less intuitive |
| Set operations | set1.union(set2) == set1 |
Moderate | Moderate |
Conclusion
The == operator is the most efficient and readable method for checking set equality. Use issubset() when you need to understand the relationship between sets, and symmetric_difference() when you want to see the actual differences between sets.
