- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program To Check Two Set Are Equal
Python is an interpreted, object–oriented, high level, programming language with dynamic semantics. Developed by Gudio Van Rossum in 1991. It supports multiple programming paradigms, including structured, object-oriented and functional programming.
What Is A Set?
Set is an unordered collection data type that iterable, mutable and has no duplicate element. Set is represented by {}. Set is highly optimized method for checking whether a specific element is present in the set.
Sets are created by placing all the items or elements inside curly brackets {}, separated by comma, or by using built in “set ()” function.
It can have various types of number of items and they may be of different types like integer, float, tuple, string etc.
For instance -
Sets are mutable that mean we can add, remove and repeat an element into it. It allows indexing and slicing like strings an element from a list by using different operators.
name={"ahana","jacob","yana","ankush","umang"}
Mixed set −
name= {1,"ahana",2, "jacob",3,"yana",4,"ankush",5,"umang"}
How To Check If Two Set Are Equal
Here, we have given two list of numbers and we have to check if that list is equal, by using different methods. There four methods to remove a subset from a list.
By using “==” operator
By using “all()”
By using “issubset()”
By using “symmetric_difference()
To check if two sets are equal, there are several methods that can be used in Python.
The first method is to use the “==” operator. This will determine if both sets have the same elements and order of elements. If so, then they are equal; otherwise, they are not equal.
Another option for checking set equality is using the all() function. This will compare each element in one set with its corresponding element in the other set and return True only if all of them match exactly.
The third way to check if two sets are equal is by using issubset(). This function returns True only if one set is a subset of another—that is, it contains all elements of the other set.
Finally, you can use the symmetric_difference() function to determine if two sets are equal. This will return an empty set if both sets are equal; otherwise, it will return a non-empty set containing the elements that appear in one but not both of the sets.
By Using “= =” Operator
The “==” operator is used to compare the value or equality of two object. They are also called “Relational operator”. If the value of two operands are equal, then the condition becomes true (a==b). If the value of two operands is not equal, then the condition becomes false (a is not equal to b).
Example
In the following example, we are using == operator to compare the value or equality of two object and compared name==rollno
name= {1,2,3,4,5} rollno= {1,2,3,4,5} print(name==rollno)
Output
On executing the above program, we get the following output as shown, we have compared name==rollno and their output comes as true because they have same set of elements.
True
Example
The example given below uses the “== operator” to compare the value or equality of two object.
name={"karan","kunnal","aditi","janshna"} rollno={1,2,3,4,5} print(name==rollno)
Output
On executing the above program, we see the output as below, and it returns false because it does not have same set of elements.
False
By Using All()
The all()function returns True, if all items in an iterable are true ,otherwise it returns false. If the iterable object is empty, the all()function also return true.
Example
In the following example we are using all() function in the program to to check if two sets are equal or not. This program checks if all elements of set num1 are equal. If so, "all elements are equal" is printed, otherwise "all elements are not equal" is printed.
num1={1,2,3,4,5} result=all(element==num1 for element in num1) if (result): print("all the elements are equal are equal") else: print("all the elements are not equal")
Output
On executing the above program, we see that the following example is displayed on the web-browser.
all the elements are not equal
Example
The following example checks if all elements of a set are equal to each other. Checkset: num1={1,1,1}. A result variable is created and assigned a boolean value based on whether all elements of num1 are equal to each other. If so, the result is True and it prints "all elements are equal". Otherwise, the result is False, printing "All elements are not equal".
num1={1,1,1} result=all(element==num1 for element in num1) if (result): print("all the elements are equal are equal") else: print("all the elements are not equal")
Output
all the elements are equal
By Using Issubset()
The issubset() method, takes a single argument (b-a) as a superset of “a” which means “b” has all subset of “a”. If the value of two operands are equal, then the condition becomes true (a==b). If the value of two operands is not equal, then the condition becomes false (a ≠ b).
Example
The following example uses issubset() method to know whether two sets are equal or not.
a={1,2,3} b={1,2,3,4,5} print(a.issubset(b))
Output
On executing the above program, we see the output as follow -
True
Example
a={1,2,3} b={1,2,3,4,5} print(b.issubset(a))
Output
Upon execution, the above program returns false, as set “b” is not a subset of “a”.
False
Here, we have two programs in both we have used issubset() method to check if “a” and “b” are subset of each other. Since all element of “a” are present in “b”. so, output comes as true. On other hand, set “b” is not a subset of “a”. Thus, we get the false with issubset(a).
By Using symmetric_difference()
The function symmetric_difference() returns a set that is the difference between two sets. The difference between two sets in python is equal to the difference between the number of elements in two sets. The symmetric_difference will return one set with zero elements. Then we can check if the length of both sets is equal.
Example
This example tests whether two sets num1 and num2 are equal. Find the difference between two sets using the symmetric_difference method.
If this difference is zero, "Both sets are identical" is printed, indicating that both contain the same elements. Otherwise it prints "sets are not equal".
num1={1,2,3} num2={1,2,3,4,5} if len(num1.symmetric_difference(num2))== 0: print("both sets are equal") else: print("sets are not equal")
Output
sets are not equal
Example
symmetric_difference method is used in the following example to find the difference between two sets, here, the difference is not zero. Thus, it returns false.
num1={1,2,3,4,5} num2={1,2,3,4,5} if len(num1.symmetric_difference(num2))== 0: print("both sets are equal") else: print("sets are not equal")
Output
On executing the above program, we get the following output as the difference between both the sets is not zero, which means they are not equal.
both sets are equal
Conclusion
In this article, we have briefly discussed about the four different methods which are used to check if two set are equal or not.