Python - Step Frequency of elements in List
Last Updated :
15 Mar, 2023
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 this task can be performed.
Method #1 : Using loop + defaultdict() The combination of above functions can be used to perform this task. In this, we just initialize list with a default value and increment its frequency using a loop.
Python3
# Python3 code to demonstrate
# Step Frequency of elements in List
# using loop + defaultdict()
from collections import defaultdict
# Initializing loop
test_list = ['gfg', 'is', 'best', 'gfg', 'is', 'life']
# printing original list
print("The original list is : " + str(test_list))
# Step Frequency of elements in List
# using loop + defaultdict()
res_d = defaultdict(int)
res = []
for ele in test_list:
res_d[ele] += 1
res.append(res_d[ele])
# printing result
print ("Step frequency of elements is : " + str(res))
OutputThe original list is : ['gfg', 'is', 'best', 'gfg', 'is', 'life']
Step frequency of elements is : [1, 1, 1, 2, 2, 1]
Time complexity: O(n), where n is the number of elements in the input list test_list.
Auxiliary space: O(m), where m is the number of unique elements in the input list test_list. The defaultdict data structure is used in the code, which may occupy a space equivalent to the number of unique elements in the input list.
Method #2 : Using list comprehension + enumerate() The combination of above functions can be used to solve this problem. In this we just iterate and store the counter using enumerate().
Python3
# Python3 code to demonstrate
# Step Frequency of elements in List
# using list comprehension + enumerate()
from collections import defaultdict
# Initializing loop
test_list = ['gfg', 'is', 'best', 'gfg', 'is', 'life']
# printing original list
print("The original list is : " + str(test_list))
# Step Frequency of elements in List
# using list comprehension + enumerate()
res = [test_list[ : idx + 1].count(ele) for (idx, ele) in enumerate(test_list)]
# printing result
print ("Step frequency of elements is : " + str(res))
Output : The original list is : ['gfg', 'is', 'best', 'gfg', 'is', 'life']
Step frequency of elements is : [1, 1, 1, 2, 2, 1]
Time Complexity: O(n^2)
Auxiliary Space: O(n)
Method #3 : Using for loop + count() method
Python3
# Python3 code to demonstrate
# Step Frequency of elements in List
# Initializing loop
test_list = ['gfg', 'is', 'best', 'gfg', 'is', 'life']
# printing original list
print("The original list is : " + str(test_list))
# Step Frequency of elements in List
res = []
for ele in range(0,len(test_list)):
res.append(test_list[:ele+1].count(test_list[ele]))
# printing result
print ("Step frequency of elements is : " + str(res))
OutputThe original list is : ['gfg', 'is', 'best', 'gfg', 'is', 'life']
Step frequency of elements is : [1, 1, 1, 2, 2, 1]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the new res list
Method #4 : Using operator.countOf() method
Python3
# Python3 code to demonstrate
# Step Frequency of elements in List
import operator as op
# Initializing loop
test_list = ['gfg', 'is', 'best', 'gfg', 'is', 'life']
# printing original list
print("The original list is : " + str(test_list))
# Step Frequency of elements in List
res = []
for ele in range(0,len(test_list)):
res.append(op.countOf(test_list[:ele+1],test_list[ele]))
# printing result
print ("Step frequency of elements is : " + str(res))
OutputThe original list is : ['gfg', 'is', 'best', 'gfg', 'is', 'life']
Step frequency of elements is : [1, 1, 1, 2, 2, 1]
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Python - List Frequency of Elements We are given a list we need to count frequencies of all elements in given list. For example, n = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4] we need to count frequencies so that output should be {4: 4, 3: 3, 2: 2, 1: 1}.Using collections.Countercollections.Counter class provides a dictionary-like structure that
2 min read
Python | Frequency grouping of list elements Sometimes, while working with lists, we can have a problem in which we need to group element along with it's frequency in form of list of tuple. Let's discuss certain ways in which this task can be performed. Method #1: Using loop This is a brute force method to perform this particular task. In this
6 min read
Python - Restrict Elements Frequency in List Given a List, and elements frequency list, restrict frequency of elements in list from frequency list. Input : test_list = [1, 4, 5, 4, 1, 4, 4, 5, 5, 6], restrct_dict = {4 : 3, 1 : 1, 6 : 1, 5 : 1} Output : [1, 4, 5, 4, 4, 6] Explanation : Limit of 1 is 1, any occurrence more than that is removed.
5 min read
Sort List Elements by Frequency - Python Our task is to sort the list based on the frequency of each element. In this sorting process, elements that appear more frequently will be placed before those with lower frequency. For example, if we have: a = ["Aryan", "Harsh", "Aryan", "Kunal", "Harsh", "Aryan"] then the output should be: ['Aryan'
3 min read
Python - Elements frequency in Tuple Given a Tuple, find the frequency of each element. Input : test_tup = (4, 5, 4, 5, 6, 6, 5) Output : {4: 2, 5: 3, 6: 2} Explanation : Frequency of 4 is 2 and so on.. Input : test_tup = (4, 5, 4, 5, 6, 6, 6) Output : {4: 2, 5: 2, 6: 3} Explanation : Frequency of 4 is 2 and so on.. Method #1 Using def
7 min read