Python - Values Frequency Index List
Last Updated :
21 Apr, 2023
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))
OutputThe 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
Similar Reads
Python - Index Frequency Alphabet List We are given a list we need to count the frequency of each alphabets from the list. For Example we have a list a = ['a', 'b', 'a', 'c', 'b', 'a'] .The output should be having count of each alphabets = {'a':3, 'b':2 ,'c':1 } . For finding frequency of each alphabet we can use dictionary, Counter clas
2 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
Python - Keys Values equal frequency Given a dictionary, count instances where keys are equal to values. Input : test_dict = {5:5, 8:9, 7:8, 1:2, 10:10, 4:8} Output : 2 Explanation : At 2 instances, keys are equal to values.Input : test_dict = {5:4, 8:9, 7:8, 1:2, 10:10, 4:8} Output : 1 Explanation : At 1 instance, key is equal to valu
4 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 - Step Frequency of elements in List Sometimes, while working with Python, we can have a problem in which we need to compute frequency in list. This is quite common problem and can have usecase in many domains. But we can atimes have problem in which we need incremental count of elements in list. Let's discuss certain ways in which thi
4 min read
Python - Ways to find indices of value in list In Python, it is common to locate the index of a particular value in a list. The built-in index() method can find the first occurrence of a value. However, there are scenarios where multiple occurrences of the value exist and we need to retrieve all the indices. Python offers various methods to achi
3 min read