Open In App

Python – Values Frequency Index List

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Values Frequency Index List
# Using loop
 
# initializing list
test_list = [('Gfg', 3), ('is', 3), ('best', 1), ('for', 5), ('geeks', 1)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Values Frequency Index List
# Using loop
res = [0 for _ in range(6)]
for ele in test_list:
    res[ele[1]] = res[ele[1]] + 1
 
# printing result
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




# Python3 code to demonstrate working of
# Values Frequency Index List
# Using Counter() + list comprehension
from collections import Counter
 
# initializing list
test_list = [('Gfg', 3), ('is', 3), ('best', 1), ('for', 5), ('geeks', 1)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Values Frequency Index List
# Using Counter() + list comprehension
cntr = Counter(ele[1] for ele in test_list)
res = [cntr[idx] for idx in range(6)]
 
# printing result
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
 
# initializing list
test_list = [('Gfg', 3), ('is', 3), ('best', 1), ('for', 5), ('geeks', 1)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Values Frequency Index List
# Using defaultdict
freq_dict = defaultdict(int)
for _, idx in test_list:
    freq_dict[idx] += 1
 
# converting defaultdict to list
res = [freq_dict.get(i, 0) for i in range(6)]
 
# printing result
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
Previous
Next
Share your thoughts in the comments
Similar Reads