Home »
Python »
Python programs
Python program to extract all symmetric tuples
Here, we need to extract all the symmetric tuples from a list consisting of multiple tuples using the Python program.
Submitted by Shivang Yadav, on September 11, 2021
Here, we are given a list of tuples with integer values. And we need to find all tuples which are symmetric in the tuple.
Before going forward with the problem let's recap some topics which we will need to understand in order to solve the problem easily.
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.
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().
# Python program to extract
# all symmetric tuples from list of tuples
# Creating and Printing list of tuples
myList = [(3, 4), (5, 1), (1, 5), (7, 4)]
print("All tuples of the tuple list : " + str(myList))
# Extracting all Symmetric tuples from list
temp = set(myList) & {(b, a) for a, b in myList}
symTupleSet = {(a, b) for a, b in temp if a < b}
# Printing Symmetric tuples
print("All tuples that are symmetric : " + str(symTupleSet))
Output:
All tuples of the tuple list : [(3, 4), (5, 1), (1, 5), (7, 4)]
All tuples that are symmetric : {(1, 5)}
Method 2:
Another method to solve the problem is by using a counter to check for equal tuples. And then print tuples accordingly.
# Python program to extract
# all Symmetric tuples from list of tuples
from collections import Counter
# Creating and Printing list of tuples
myList = [(3, 4), (5, 1), (1, 5), (7, 4)]
print("All tuples of the tuple list : " + str(myList))
# Extracting all Symmetric tuples from list
temp = [(sub[1], sub[0]) if sub[0] < sub[1] else sub for sub in myList]
cnts = Counter(temp)
symTuples = [key for key, val in cnts.items() if val == 2]
# Printing Symmetric tuples
print("All tuples that are symmetric : " + str(symTuples))
Output:
All tuples of the tuple list : [(3, 4), (5, 1), (1, 5), (7, 4)]
All tuples that are symmetric : [(5, 1)]
Python Tuple Programs »