Write a Python program to Unique Tuple Frequency (Order Irrespective)


The program is written in Python and performs the following operations:

  • Defines a list of tuples called "val".
  • Prints the original list of tuples.
  • Defines a new list "l" by using a set comprehension, which is a concise way to create a set in Python.
  • The set comprehension iterates through each tuple in "val", sorts the tuple in ascending order, converts the sorted tuple to a list, and finally converts the list back to a tuple.
  • The set comprehension then converts the list of tuples to a set, which removes any duplicates.
  • Defines a variable "res" and assigns the length of the set "l" to it.
  • Prints the set of unique tuples, and the number of unique tuples in the list "val".

The final output of the program will be the set of unique tuples in the list "val" and the number of unique tuples in the list. The set comprehension will remove any duplicate tuples from the list and the length of the set will give the number of unique tuples in the list.

Source Code

val = [(3, 4), (1, 2), (4, 3), (5, 6)]
 
print("Original list : ",val)
 
l = list(set(tuple(sorted(s)) for s in val))
res = len(l)
print("Unique tuples Frequency :",l)
print("Unique tuples Frequency Count : " ,res)

Output

Original list :  [(3, 4), (1, 2), (4, 3), (5, 6)]
Unique tuples Frequency : [(1, 2), (3, 4), (5, 6)]
Unique tuples Frequency Count :  3


Example Programs