Q:

Python program to extract all symmetric tuples

belongs to collection: Python Tuple Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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)]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to perform cross tuple summation gr... >>
<< Python program to concatenate tuple elements by de...