Open In App

Python – Restrict Elements Frequency in List

Improve
Improve
Like Article
Like
Save
Share
Report

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. Similar with all elements.
Input : test_list = [1, 4, 5, 4, 1, 4, 4, 5, 5, 6], restrct_dict = {4 : 2, 1 : 1, 6 : 1, 5 : 1} 
Output : [1, 4, 5, 4, 6] 
Explanation : Limit of 4 is 3, any occurrence more than that is removed. Similar with all elements. 

Method 1 : Using loop + defaultdict()

In this, we iterate for elements and and maintain lookup counter for each element using defaultdict(), if any element exceeds restrict dict, that element is not added then onwards.

Python3




# Python3 code to demonstrate working of
# Restrict Elements Frequency in List
# Using loop + defaultdict()
from collections import defaultdict
 
# initializing list
test_list = [1, 4, 5, 4, 1, 4, 4, 5, 5, 6]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing restrct_dict
restrct_dict = {4 : 3, 1 : 1, 6 : 1, 5 : 2}
 
res = []
lookp = defaultdict(int)
for ele in test_list:
    lookp[ele] += 1
     
    # move to next ele if greater than restrct_dict count
    if lookp[ele] > restrct_dict[ele]:
        continue
    else:
        res.append(ele)
 
# printing results
print("Filtered List : " + str(res))


Output

The original list is : [1, 4, 5, 4, 1, 4, 4, 5, 5, 6]
Filtered List : [1, 4, 5, 4, 4, 5, 6]

Time Complexity: O(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 #2: Using reduce():
Algorithm :

  1. Create an input list ‘test_list’ with the given elements.
  2. Create a dictionary ‘restrct_dict’ with the restriction values for each element.
  3. Initialize an empty dictionary ‘lookp’ to keep track of the frequency of each element in ‘test_list’.
  4. Define a function ‘filter_list’ that takes an accumulator, an element of ‘test_list’, and applies the restriction to that element based on the ‘restrct_dict’ and updates the accumulator with the element if it meets the restriction.
  5. Use the reduce() function from functools module to apply the ‘filter_list’ function to each element of the ‘test_list’ and get a filtered list.
  6. Print the filtered list.

Python3




from collections import defaultdict
from functools import reduce
 
# initializing list
test_list = [1, 4, 5, 4, 1, 4, 4, 5, 5, 6]
 
# initializing restrct_dict
restrct_dict = {4: 3, 1: 1, 6: 1, 5: 2}
# printing original list
print("The original list is : " + str(test_list))
  
lookp = defaultdict(int)
 
# function to filter the list based on restriction dictionary
def filter_list(acc, ele):
    lookp[ele] += 1
    if lookp[ele] <= restrct_dict.get(ele, float('inf')):
        return acc + [ele]
    return acc
 
# using reduce() to apply filter_list on each element of test_list
res = reduce(filter_list, test_list, [])
 
# printing results
print("Filtered List : " + str(res))
#This code is contrinuted by Pushpa.


Output

The original list is : [1, 4, 5, 4, 1, 4, 4, 5, 5, 6]
Filtered List : [1, 4, 5, 4, 4, 5, 6]

Time Complexity: O(n), where n is the length of the input list ‘test_list’. This is because we are iterating through each element in the list once to apply the filter.

Auxiliary Space: O(m), where m is the number of distinct elements in the input list ‘test_list’. This is because we are using a dictionary to keep track of the frequency of each element in the list. The space required by the dictionary is proportional to the number of distinct elements in the list.

Method #3 : Using count() method

Approach

  1. Initiate a for loop from i = 0 to len(test_list)
  2. Slice test_list from beginning to i index and check whether the count of test_list[i] is less than the value of key test_list[i] in restrct_dict (using count() method)
  3. If yes append test_list[i] to output list res
  4. Display res

Python3




# Python3 code to demonstrate working of
# Restrict Elements Frequency in List
 
# initializing list
test_list = [1, 4, 5, 4, 1, 4, 4, 5, 5, 6]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing restrct_dict
restrct_dict = {4 : 3, 1 : 1, 6 : 1, 5 : 2}
res=[]
for i in range(0,len(test_list)):
    if test_list[:i].count(test_list[i])<restrct_dict[test_list[i]]:
        res.append(test_list[i])
 
# printing results
print("Filtered List : " + str(res))


Output

The original list is : [1, 4, 5, 4, 1, 4, 4, 5, 5, 6]
Filtered List : [1, 4, 5, 4, 4, 5, 6]

Time Complexity : O(N) N – length of test_list
Auxiliary Space : O(N) N – length of res

Method #4 : Using operator.countOf() method

Approach

  1. Initiate a for loop from i = 0 to len(test_list)
  2. Slice test_list from the beginning to i index and check whether the count of test_list[i] is less than the value of key test_list[i] in restrct_dict (using operator.countOf() method)
  3. If yes append test_list[i] to output list res
  4. Display res

Python3




# Python3 code to demonstrate working of
# Restrict Elements Frequency in List
 
# initializing list
test_list = [1, 4, 5, 4, 1, 4, 4, 5, 5, 6]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing restrct_dict
restrct_dict = {4 : 3, 1 : 1, 6 : 1, 5 : 2}
res=[]
import operator
for i in range(0,len(test_list)):
    if operator.countOf(test_list[:i],test_list[i])<restrct_dict[test_list[i]]:
        res.append(test_list[i])
 
# printing results
print("Filtered List : " + str(res))


Output

The original list is : [1, 4, 5, 4, 1, 4, 4, 5, 5, 6]
Filtered List : [1, 4, 5, 4, 4, 5, 6]

Time Complexity : O(N) N – length of test_list
Auxiliary Space : O(N) N – length of res



Last Updated : 08 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads