Python – Unique Tuple Frequency (Order Irrespective)
Last Updated : 08 May, 2023
Given tuple list, extract the frequency of unique tuples in list order irrespective.
Input : test_list = [(3, 4), (1, 2), (4, 3), (3, 4)] Output : 2 Explanation : (3, 4), (4, 3), (3, 4) makes 1 and (1, 2) is 2nd unique element.
Input : test_list = [(3, 7), (1, 2), (4, 3), (5, 6)] Output : 4 Explanation : All are different in any order.
Method #1 : Using tuple() + generator expression + sorted() + len()
The combination of above functions can be used to solve this problem. In this, we perform the task of sorting using sorted(), to remove order constraints. The len() is used to compute size.
Python3
test_list = [( 3 , 4 ), ( 1 , 2 ), ( 4 , 3 ), ( 5 , 6 )] print ( "The original list is : " + str (test_list)) res = len ( list ( set ( tuple ( sorted (sub)) for sub in test_list))) print ( "Unique tuples Frequency : " + str (res)) |
Output : The original list is : [(3, 4), (1, 2), (4, 3), (5, 6)] Unique tuples Frequency : 3
Time Complexity: O(nlogn) where n is the number of elements in the list
Auxiliary Space: O(n), where n is the number of elements in the list
Method #2: Using map() + sorted() + tuple() + set() + len()
The combination of the above functions can be used to solve this problem. In this, we perform the task of extending sorting logic and tuple conversion using map(), set() is used to eliminate duplicates and len() is used to find the length of the container.
Python3
test_list = [( 3 , 4 ), ( 1 , 2 ), ( 4 , 3 ), ( 5 , 6 )] print ( "The original list is : " + str (test_list)) res = len ( list ( set ( map ( tuple , map ( sorted , test_list))))) print ( "Unique tuples Frequency : " + str (res)) |
Output : The original list is : [(3, 4), (1, 2), (4, 3), (5, 6)] Unique tuples Frequency : 3
Method#3: Using Recursive method.
This method works by recursively calling itself with the remaining elements of the input list after the first element is removed. It then checks if the first element is unique by comparing it to the sorted version of each of the remaining elements. If the first element is unique, it adds 1 to the frequency returned by the recursive call. If it is not unique, it simply returns the frequency returned by the recursive call. The base case is when the input list is empty, in which case the frequency is 0.
Python3
def unique_tuple_frequency(test_list): if len (test_list) = = 0 : return 0 rest_freq = unique_tuple_frequency(test_list[ 1 :]) for t in test_list[ 1 :]: if sorted (test_list[ 0 ]) = = sorted (t): return rest_freq return rest_freq + 1 test_list = [( 3 , 4 ), ( 1 , 2 ), ( 4 , 3 ), ( 5 , 6 )] print ( "The original list is :" + str (test_list)) res = unique_tuple_frequency(test_list) print ( "Unique tuples Frequency : " + str (res)) |
Output The original list is :[(3, 4), (1, 2), (4, 3), (5, 6)] Unique tuples Frequency : 3
Time Complexity: O(N * MlogM)
Sorting each tuple: O(MlogM) where M is the size of the tuple.
Comparing each tuple to the first tuple: O(N * M) where N is the length of the input list and M is the size of the tuple.
Overall time complexity: O(N * MlogM)
Auxiliary Space: O(N * M)
Recursive call stack: O(N)
Creating a new list of remaining elements for each recursive call: O(N * M)
Method #4: Using dictionary and loop
Steps:
- Initialize an empty dictionary “freq_dict” to store the frequency of each unique tuple.
- Loop through each tuple in the input list “test_list”.
- Sort the tuple using sorted() and convert it to a tuple again using tuple(). This will give us a sorted version of the tuple, which we can use to check if it is unique.
- Check if the sorted tuple exists as a key in the “freq_dict”.
- If it does, increment the value of the corresponding key in “freq_dict”.
- If it does not, add the sorted tuple as a key to “freq_dict” with a value of 1.
- Finally, return the length of “freq_dict”, which will give us the number of unique tuples in “test_list”.
Python3
def unique_tuple_frequency(test_list): freq_dict = {} for tup in test_list: sorted_tup = tuple ( sorted (tup)) if sorted_tup in freq_dict: freq_dict[sorted_tup] + = 1 else : freq_dict[sorted_tup] = 1 return len (freq_dict) test_list = [( 3 , 4 ), ( 1 , 2 ), ( 4 , 3 ), ( 5 , 6 )] print ( "The original list is :" + str (test_list)) res = unique_tuple_frequency(test_list) print ( "Unique tuples Frequency : " + str (res)) |
Output The original list is :[(3, 4), (1, 2), (4, 3), (5, 6)] Unique tuples Frequency : 3
Time complexity: O(nlogn), where n is the length of the input list “test_list”.
Auxiliary space: O(n), where n is the length of the input list “test_list”.
Similar Reads
Python | Extract unique tuples from list, Order Irrespective
Sometimes, while working with data, we can have a problem in which we need to check for similar records and eradicate them. When elements are ordered, this case has been discussed before. But sometimes, we might have to ignore the order and has to be removed in case similar elements occur. Let's dis
5 min read
Python - Tuple List intersection (Order irrespective)
Given list of tuples, perform tuple intersection of elements irrespective of their order. Input : test_list1 = [(3, 4), (5, 6)], test_list2 = [(5, 4), (4, 3)] Output : {(3, 4)} Explanation : (3, 4) and (4, 3) are common, hence intersection ( order irrespective). Input : test_list1 = [(3, 4), (5, 6)]
6 min read
Python - Test for Unique Frequencies
Given a list, find if frequencies of each element of values are in itself unique values. Input : test_list = [4, 3, 2] Output : False Explanation : All have 1 as frequency, hence duplicacy, hence False Input : test_list = [4, 3, 3, 2, 2, 2] Output : True Explanation : 1, 2, 3 are frequencies of 4, 3
7 min read
Python | Get unique tuples from list
Sometimes, while working with Python list, we can come across a problem in which we require to find the unique occurrences of list. Having elementary data types is easy to handle, but sometime, we might have complex data types and the problem becomes new in that cases. Let's discuss certain ways in
3 min read
Python - Order Tuples by List
Sometimes, while working with Python tuples, we can have a problem in which we need to perform ordering of all the tuples keys using external list. This problem can have application in data domains such as Data Science. Let's discuss certain ways in which this task can be performed. Input : test_lis
7 min read
Python - Unique Kth positioned tuples
Sometimes, while working with Python records, we can have a problem in which we need to extract only the unique tuples, based on some particular index of tuples. This kind of problem can have applications in domains such as web development. Let's discuss certain ways in which this task can be perfor
8 min read
Python | Finding frequency in list of tuples
In python we need to handle various forms of data and one among them is list of tuples in which we may have to perform any kind of operation. This particular article discusses the ways of finding the frequency of the 1st element in list of tuple which can be extended to any index. Let's discuss cert
6 min read
Python - Maximum frequency in Tuple
Sometimes, while working with Python tuples, we can have a problem in which we need to find the maximum frequency element in the tuple. Tuple, being quite a popular container, this type of problem is common across the web development domain. Let's discuss certain ways in which this task can be perfo
5 min read
Python | How to get unique elements in nested tuple
Sometimes, while working with tuples, we can have a problem in which we have nested tuples and we need to extract elements that occur singly, i.e are elementary. This kind of problem can have applications in many domains. Let's discuss certain ways in which this problem can be solved. Method #1: Usi
7 min read
Python - Values frequencies of key
Sometimes, while working with Python dictionary lists, one can have a problem in which we require to find the frequency of all the values occurring in a specific key in dictionary list. This can have application across many domains including web development. Let's discuss certain ways in which this
7 min read