Python – Values Frequency Index List
Sometimes, while working with Python tuples, we can have a problem in which we need to extract the frequency of each value in tuple. This has been solved earlier. We can have a modification in which we need to create list in which index represents the key and value of it represents the frequency of that index number. This kind of problem can have applications in competitive programming domain. Let’s discuss certain ways in which we need to solve this problem.
Input : test_list = [(‘Gfg’, 1), (‘is’, 1), (‘best’, 1), (‘for’, 1), (‘geeks’, 1)]
Output : [0, 5, 0, 0, 0, 0]
Input : test_list = [(‘Gfg’, 5), (‘is’, 5)]
Output : [0, 0, 0, 0, 0, 2]
Method #1: Using loop This is brute force approach by which this task can be performed. In this, we perform the task of assigning frequency by iterating and assigning pre-initialized list.
Python3
test_list = [( 'Gfg' , 3 ), ( 'is' , 3 ), ( 'best' , 1 ), ( 'for' , 5 ), ( 'geeks' , 1 )]
print ("The original list is : " + str (test_list))
res = [ 0 for _ in range ( 6 )]
for ele in test_list:
res[ele[ 1 ]] = res[ele[ 1 ]] + 1
print ("The Frequency list : " + str (res))
|
Output :
The original list is : [('Gfg', 3), ('is', 3), ('best', 1), ('for', 5), ('geeks', 1)]
The Frequency list : [0, 2, 0, 2, 0, 1]
Time complexity: O(n), where n is the length of the input list test_list.
Auxiliary Space: O(1), as we are only using a fixed-size list of length 6 to store the frequency of values, regardless of the size of the input list.
Method #2: Using Counter() + list comprehension The combination of above functionalities is used to solve this problem. In this, we perform the task of computing frequencies using Counter() and rendering in list is done by list comprehension.
Python3
from collections import Counter
test_list = [( 'Gfg' , 3 ), ( 'is' , 3 ), ( 'best' , 1 ), ( 'for' , 5 ), ( 'geeks' , 1 )]
print ("The original list is : " + str (test_list))
cntr = Counter(ele[ 1 ] for ele in test_list)
res = [cntr[idx] for idx in range ( 6 )]
print ("The Frequency list : " + str (res))
|
Output :
The original list is : [('Gfg', 3), ('is', 3), ('best', 1), ('for', 5), ('geeks', 1)]
The Frequency list : [0, 2, 0, 2, 0, 1]
Time complexity: O(N), where N is the length of the input list.
Auxiliary space: O(1)
Method #3: Using defaultdict
Initialize an empty defaultdict with int as the default factory function.
Iterate through the tuples in the given list and for each tuple:
a. Access the second element of the tuple and increment the count for the corresponding key in the defaultdict.
Convert the defaultdict into a list of counts using the range function and the get method of the defaultdict.
Print the frequency list.
Python3
from collections import defaultdict
test_list = [( 'Gfg' , 3 ), ( 'is' , 3 ), ( 'best' , 1 ), ( 'for' , 5 ), ( 'geeks' , 1 )]
print ( "The original list is : " + str (test_list))
freq_dict = defaultdict( int )
for _, idx in test_list:
freq_dict[idx] + = 1
res = [freq_dict.get(i, 0 ) for i in range ( 6 )]
print ( "The Frequency list : " + str (res))
|
Output
The original list is : [('Gfg', 3), ('is', 3), ('best', 1), ('for', 5), ('geeks', 1)]
The Frequency list : [0, 2, 0, 2, 0, 1]
Time Complexity: O(n), where n is the number of tuples in the given list.
Auxiliary Space: O(k), where k is the number of unique indices
Last Updated :
21 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...