Example:
tuple = ("python", "includehelp", 43, 54.23)
Check if Two Lists of tuples are identical or not
We are given two tuple lists consisting of integer elements. We need to create a Python program to check whether the given tuple lists are identical i.e. consist of the same set of elements and the same position or not. And return true or false accordingly.
Input:
tupList1 = [(2, 9) ,(5, 6)] ; tupList2 = [(2, 9) ,(5, 6)]
Output:
true
Input:
tupList1 = [(2, 9) ,(5, 6)] ; tupList2 = [(1, 5) ,(5, 6)]
Output:
false
Method 1:
One method that can accomplish that task is using the equality operator '=='. It returns a boolean value based on the equality of the two collections in python.
Output:
Method 2:
Another method that can accomplish the task is by using cmp() method from the python library. This method returns the difference of both collections and 0 (falsy value ) if they are identical.
Using this result we can check if both tuple lists are identical or not.
Note: The cmp() method is supported in Python 2.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer