Example:
tuple = ("python", "includehelp", 43, 54.23)
List is a sequence data type. It is mutable as its values in the list can be modified. It is a collection of an ordered set of values enclosed in square brackets []
Example:
list = [3 ,1, 5, 7]
Symmetric Tuples
Symmetric tuples are tuples that have the same elements but their arrangement is opposite to each other.
Example: (2, 5) and (5, 2)
Now, let's gets back to our problem where we need to find all the tuples that are exactly symmetric.
Extracting all Symmetric Tuples
In this program, we have a list of tuples out of some tuples that are symmetric to each other. And we need to extract all the tuples from the list of tuples that are symmetric.
Input:
[(3, 4), (5, 1), (1, 5), (7, 4)]
Output:
{(5, 1)}
To extract all such elements we need to find all such pairs that make two symmetric tuples in the list. For, this we will compare the tuples with their reverse value. In python, we can use different ways and methods to perform this task. Let's see some methods in action.
Method 1:
One method to solve the problem is by simply creating reverse pairs for tuples and then comparing them using dictionary comparison in order to check for the symmetric tuples. And to avoid duplicates, we will be using set().
Output:
Method 2:
Another method to solve the problem is by using a counter to check for equal tuples. And then print tuples accordingly.
Output:
need an explanation for this answer? contact us directly to get an explanation for this answer