Given dual tuples, get a count of unique keys for each value present in the tuple.
Input : test_list = [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Output : {4: 4, 2: 3, 1: 2}
Explanation : 3, 2, 8 and 10 are keys for value 4.Input : test_list = [(3, 4), (1, 2), (8, 1), (9, 1), (8, 4), (10, 4)]
Output : {4: 3, 2: 1, 1: 2}
Explanation : 3, 8 and 10 are keys for value 4.
Method #1 : Using loop + defaultdict()
In this, we iterate for each tuple element, having key as tuple value, and increment it with each different key encountered in the dictionary value list. Next, frequency is computed using another iteration by getting length of mapped value list, converting to set to get a unique count.
Python3
# Python3 code to demonstrate working of # Unique keys count for Value in Tuple List # Using loop + defaultdict() from collections import defaultdict # initializing list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 2 , 4 ), ( 8 , 2 ), ( 7 , 2 ), ( 8 , 1 ), ( 9 , 1 ), ( 8 , 4 ), ( 10 , 4 )] # printing original list print ( "The original list is : " + str (test_list)) res = defaultdict( list ) for sub in test_list: # getting all keys to values res[sub[ 1 ]].append(sub[ 0 ]) res = dict (res) res_dict = dict () for key in res: # getting unique key counts for each value res_dict[key] = len ( list ( set (res[key]))) # printing result print ( "Unique keys for values : " + str (res_dict)) |
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)] Unique keys for values : {4: 4, 2: 3, 1: 2}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using loop + defaultdict() + Counter()
In this, in order to reduce a computation loop, the list is constructed dynamically having just unique values. Then Counter() is used to get unique value dictionary.
Python3
# Python3 code to demonstrate working of # loop + defaultdict() + Counter() # Using loop + defaultdict() + Counter() from collections import defaultdict, Counter # initializing list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 2 , 4 ), ( 8 , 2 ), ( 7 , 2 ), ( 8 , 1 ), ( 9 , 1 ), ( 8 , 4 ), ( 10 , 4 )] # printing original list print ( "The original list is : " + str (test_list)) mem_dict = defaultdict( list ) res = [] for sub in test_list: # if not in dict, add value if sub[ 0 ] not in mem_dict[sub[ 1 ]]: mem_dict[sub[ 1 ]].append(sub[ 0 ]) res.append(sub[ 1 ]) # getting frequency res = dict (Counter(res)) # printing result print ( "Unique keys for values : " + str (res)) |
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)] Unique keys for values : {4: 4, 2: 3, 1: 2}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using count() method
Python3
# Python3 code to demonstrate working of # Unique keys count for Value in Tuple List # initializing list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 2 , 4 ), ( 8 , 2 ), ( 7 , 2 ), ( 8 , 1 ), ( 9 , 1 ), ( 8 , 4 ), ( 10 , 4 )] # printing original list print ( "The original list is : " + str (test_list)) x = [] for i in test_list: i = list (i) x.append(i[ 1 ]) d = dict () for i in x: d[i] = x.count(i) # printing result print ( "Unique keys for values : " + str (d)) |
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)] Unique keys for values : {4: 4, 2: 3, 1: 2}
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) additional space of size n is created
Method 4: using operator.countOf() method
Python3
# Python3 code to demonstrate working of # Unique keys count for Value in Tuple List import operator as op # initializing list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 2 , 4 ), ( 8 , 2 ), ( 7 , 2 ), ( 8 , 1 ), ( 9 , 1 ), ( 8 , 4 ), ( 10 , 4 )] # printing original list print ( "The original list is : " + str (test_list)) x = [] for i in test_list: i = list (i) x.append(i[ 1 ]) d = dict () for i in x: d[i] = op.countOf(x, i) # printing result print ( "Unique keys for values : " + str (d)) |
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)] Unique keys for values : {4: 4, 2: 3, 1: 2}
Time Complexity: O(N)
Auxiliary Space : O(N)
Method 5: Using numpy:
Algorithms :
- Initialize an empty list x.
- Iterate through each tuple in test_list.
- Convert the tuple to a list and append the second element of the list to x.
- Initialize an empty dictionary d.
- Iterate through each element i in x.
- Add a key-value pair to d where the key is i and the value is the number of times i appears in x.
- Print the resulting dictionary d.has context menu
Python3
import numpy as np # initializing list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 2 , 4 ), ( 8 , 2 ), ( 7 , 2 ), ( 8 , 1 ), ( 9 , 1 ), ( 8 , 4 ), ( 10 , 4 )] # printing original list print ( "The original list is : " + str (test_list)) # converting list of tuples to 2D numpy array arr = np.array(test_list) # extracting second column from numpy array col = arr[:, 1 ] # finding unique keys for values using numpy.unique() and numpy.bincount() functions unique, counts = np.unique(col, return_counts = True ) unique_keys_for_values = dict ( zip (unique, counts)) # printing result print ( "Unique keys for values : " + str (unique_keys_for_values)) |
Output:
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)] Unique keys for values : {1: 2, 2: 3, 4: 4}
The time complexity :O(n), where n is the length of the input list. This is because the code iterates through the list once to extract the second element of each tuple and then again to count the number of occurrences of each second element.
The space complexity :O(k), where k is the number of unique second elements in the input list. This is because the code creates a dictionary to store the count of each unique second element, which has a space complexity proportional to the number of unique second elements.
Method 6: Using dictionary comprehension + set comprehension
Python3
# importing required module from collections import defaultdict # initializing list test_list = [( 3 , 4 ), ( 1 , 2 ), ( 2 , 4 ), ( 8 , 2 ), ( 7 , 2 ), ( 8 , 1 ), ( 9 , 1 ), ( 8 , 4 ), ( 10 , 4 )] # printing original list print ( "The original list is : " + str (test_list)) # using dictionary comprehension + set comprehension # for counting unique keys for each value res_dict = {sub[ 1 ]: len ({x[ 0 ] for x in test_list if x[ 1 ] = = sub[ 1 ]}) for sub in test_list} # printing result print ( "Unique keys for values : " + str (res_dict)) # Time complexity: O(n^2) # Auxiliary space: O(n) (for res_dict) |
The original list is : [(3, 4), (1, 2), (2, 4), (8, 2), (7, 2), (8, 1), (9, 1), (8, 4), (10, 4)] Unique keys for values : {4: 4, 2: 3, 1: 2}
Time complexity: O(n^2)
Auxiliary space: O(n)