Python - Unique Tuple Frequency (Order Irrespective)


In this article, we will have input as a list of tuples and our goal will be to print the frequency of unique tuples but it will be order irrespective. Order irrespective means that a tuple (1,2,3) and (1,3,2) will be treated as same even though their order is different.

For example, consider the following −

Input

[(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]

Output

2

Explanation

The tuples at index 0,1,3 and 4 are same and hence frequency count increases by 1. Tuple at index 2 i.e. (4,5,6) is unique therefore frequency count increases by 1 again. Hence a total number of unique tuples stand at 2.

Method 1: Using a Set to Store Unique Tuples

  • The set comprehension iterates through each tuple in the "data" list.

  • Within the comprehension, we sort each tuple; consequently, new tuples emerge their elements finely ordered in ascending sequence.

  • The code executes three primary tasks − it first presents the original "data" list; then, it tallies and displays the number of unique tuples in this same "data" list importantly, it regards analogous tuples with identical elements as distinct if their order varies due to a sorting step.

Example

data = [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]

unique_tuples_set = set(tuple(sorted(item)) for item in data)

print("Input List:", data)
print("Frequency of unique tuples =", len(unique_tuples_set))

Output

Input List: [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]
Frequency of unique tuples = 2

Method 2: Using a dictionary to filter unique tuples

  • We iterate over the input list and each element which is actually a tuple transforms into a sorted one. This process involves creating a dictionary wherein − keys become the sorted tuples; values remain as original tuples.

  • Subsequently, we incorporate the sorted tuple into the dictionary as a key; simultaneously, we assign its corresponding value-the original tuple.

Example

data = [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]

unique_tuples_dict = {tuple(sorted(item)): item for item in data}

print("Input List:", data)
print("Frequency of unique tuples =", len(unique_tuples_dict))

Output

Input List: [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]
Frequency of unique tuples = 2

Method 3: Using Simple loop and Conditional Statement

  • We initiate a list, devoid of any elements; this is purposed for the housing of unique tuples.

  • We then iterate through the input list − we convert each individual tuple into a sorted one.

  • We subsequently verify the sorted tuple − if it is not already in our list of unique tuples then we shall append it.

Example

data = [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]

unique_tuples_list = []
for item in data:
   unique_tuple = tuple(sorted(item))
   if unique_tuple not in unique_tuples_list:
      unique_tuples_list.append(unique_tuple)
print("Input List:", data)
print("Frequency of unique tuples =", len(unique_tuples_list))

Output

Input List: [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]
Frequency of unique tuples = 2

Method 4: Using a Custom Function

  • Here, we delineate our distinct function − a tool designed specifically to eradicate duplicate tuples.

  • Within this function, we generate an empty list − this serves to store the unique tuples.

  • Then, we undertake the following process − iterating through the input list; converting each tuple in turn into a sorted tuple.

  • We next proceed by evaluating whether the sorted tuple already exists within our list of unique tuples − if it does not we append it.

  • Finally, we retrieve the list of unique tuples − then, by printing its length which also signifies the frequency of unique tuples we return it.

Example

data = [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]

def remove_duplicate_tuples(data_list):
   unique_tuples = []
   for item in data_list:
      unique_tuple = tuple(sorted(item))
      if unique_tuple not in unique_tuples:
         unique_tuples.append(unique_tuple)
   return unique_tuples

unique_tuples_list = remove_duplicate_tuples(data)
print("Input List:", data)
print("Frequency of unique tuples =", len(unique_tuples_list))

Output

Input List: [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]
Frequency of unique tuples = 2

Method 5: Using itertools.groupby

  • We import the groupby function from the itertools module here.

  • We then sort the input list − this process hinges on each tuple's sorted version.

  • Next, we initiate iteration through the sorted list using groupby.

  • Effectively removing duplicates, we extract the first item from each group; finally, we print the frequency.

Example

data = [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]

from itertools import groupby

def remove_duplicate_tuples_groupby(data_list):
   data_list.sort(key=lambda x: sorted(x))
   unique_tuples = [next(group) for key, group in groupby(data_list, key=lambda x: sorted(x))]
   return unique_tuples

unique_tuples_list = remove_duplicate_tuples_groupby(data)
print("Input List:", data)
print("Frequency of unique tuples =", len(unique_tuples_list))

Output

Input List: [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]
Frequency of unique tuples = 2

Method 6: Using Set and Frozenset

  • The code below actively creates a unique set named "unique_tuples_set"; it is, initially, empty.

  • A set comprehension is its tool of choice − it strides through each tuple in the "data" list.

  • Within the comprehension − each tuple undergoes conversion into a frozenset - an immutable set.

  • The code executes two primary functions − first, it prints the original "data" list; second, it calculates and outputs the count of unique tuples found within this "data" list. However due to frozensets in operation the equivalence principle is applied where tuples are considered identical if they contain similar elements, regardless of their arrangement.

Example

data = [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]

unique_tuples_set = {frozenset(item) for item in data}

print("Input List:", data)
print("Frequency of unique tuples =", len(unique_tuples_set))

Output

Input List: [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]
Frequency of unique tuples = 2

Method 7: Using a loop and a Set to Keep Track of Unique Tuples

  • We instantiate an empty set − this serves as a receptacle wherein we store unique tuples.

  • Next − we initiate an iteration through the input list.

  • Convert each tuple into a sorted version; subsequently, add it to the set.

  • Display the input list − subsequently, indicate the size of the set: this effectively demonstrates the frequency of unique tuples.

Example

data = [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]

unique_tuples = set()
for item in data:
   unique_tuple = tuple(sorted(item))
   unique_tuples.add(unique_tuple)
print("Input List:", data)
print("Frequency of unique tuples =", len(unique_tuples))

Output

Input List: [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]
Frequency of unique tuples = 2

Method 8: Using collections.Counter and list comprehension

  • Python's collections module actively enumerates the frequency of unique tuples within "data"; in particular, it uses its Counter feature.

  • Before counting, it sorts the elements within each tuple; this ensures uniform treatment of equivalent tuples even those with elements in different orders.

  • Subsequently, the code executes two actions: it prints the original "data" list; additionally, it calculates—taking into account sorted elements as equivalent tuples—the count of unique tuples within this list.

Example

data = [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]
from collections import Counter

unique_tuples_counter = Counter(tuple(sorted(item)) for item in data)

print("Input List:", data)
print("Frequency of unique tuples =", len(unique_tuples_counter))

Output

Input List: [(1, 2, 3), (2, 1, 3), (4, 5, 6), (1, 2, 3), (3, 2, 1)]
Frequency of unique tuples = 2

Conclusion

In this article we covered different approaches to printing the frequency of unique tuples inside of a list (order irrespective). The methods demonstrated were set, dictionary, list comprehension, custom function, itertools, frozenset, loops and collections. Counter with each method having its own pros and cons. The approaches not only help us find the frequency but also help us practice our general programming and python skills.

Updated on: 02-Nov-2023

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements