Open In App

Python – Step Frequency of elements in List

Last Updated : 15 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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))


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), 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))


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*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))


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)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads