Open In App

Python – Extract elements with Frequency greater than K

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a List, extract all elements whose frequency is greater than K.

Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 3, 8], K = 3 
Output : [4, 3] 
Explanation : Both elements occur 4 times. 

Input : test_list = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6], K = 2 
Output : [4, 3, 6] 
Explanation : Occur 4, 3, and 3 times respectively.

Method #1 : Using count() + loop

In this, we use count() to get the frequency, and a loop is used to iterate for each of the elements of the List. 

Python3




# Python3 code to demonstrate working of
# Extract elements with Frequency greater than K
# Using count() + loop
 
# initializing list
test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]
 
# printing string
print("The original list : " + str(test_list))
 
# initializing K
K = 2
 
res = []
for i in test_list:
     
    # using count() to get count of elements
    freq = test_list.count(i)
     
    # checking if not already entered in results
    if freq > K and i not in res:
        res.append(i)
 
# printing results
print("The required elements : " + str(res))


Output

The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]
The required elements : [4, 3]

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using list comprehension + Counter()

In this, we perform the task of counting using Counter() and the iteration part is done inside list comprehension.

Python3




# Python3 code to demonstrate working of
# Extract elements with Frequency greater than K
# Using list comprehension + Counter()
from collections import Counter
 
# initializing list
test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]
 
# printing string
print("The original list : " + str(test_list))
 
# initializing K
K = 2
 
# using list comprehension to bind result
res = [ele for ele, cnt in Counter(test_list).items() if cnt > K]
 
# printing results
print("The required elements : " + str(res))


Output

The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]
The required elements : [4, 3]

Time complexity: O(n), where n is the length of the test_list. The list comprehension + Counter() takes O(n) time
Auxiliary Space: O(n), extra space of size n is required

Method #3: Using a dictionary

In this we keep count of the elements, if the count of the element id K + 1, we add that element to the output.

Python3




test_list = [4, 6, 4, 3, 3, 4, 3, 4, 6, 6]
k = 2
unique_elems = []
freq_dict = {}
output = []
 
# printing string
print("The original list : " + str(test_list))
 
for i in test_list:
    # Append in the unique element list
    if i not in unique_elems:
        unique_elems.append(i)
        freq_dict[i] = 1
    else:
        # increment the counter if element is duplicate
        freq_dict[i] += 1
 
    # Add in the output list only once
    if freq_dict[i] == k + 1:
        output.append(i)
 
print('The required elements : ', str(output))


Output

The original list : [4, 6, 4, 3, 3, 4, 3, 4, 6, 6]
The required elements :  [4, 3, 6]

Time Complexity: O(n), where n is the length test_list. 
Auxiliary Space: O(n), where n is number of elements in output list.

Method #4: Using operator.countOf() method

Python3




# Python3 code to demonstrate working of
# Extract elements with Frequency greater than K
import operator as op
 
# initializing list
test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]
 
# printing string
print("The original list : " + str(test_list))
 
# initializing K
K = 2
unique_elements = set(test_list)
res = []
for i in unique_elements:
 
    # using operatorcountOf() to get count of elements
    if op.countOf(test_list, i) > K:
        res.append(i)
 
# printing results
print("The required elements : " + str(res))


Output

The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]
The required elements : [3, 4]

Time Complexity:O(N)
Auxiliary Space:O(N)

Method#5: using NumPy module

Python3




import numpy as np
 
test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]
K = 2
 
# use numpy unique to extract unique elements and their frequency
unique_elements, counts = np.unique(test_list, return_counts=True)
 
# extract elements with frequency greater than K
res = unique_elements[counts > K].tolist()
 
# printing results
print("The required elements : " + str(res))
#this code is contributed by Asif_Shaik


Output:

The required elements : [3, 4]

Time Complexity:O(N)
Auxiliary Space:O(N)

Method #6: Using filter() and lambda()

Algorithm:

  1. Import the Counter class from the collections module.
  2. Define the input list test_list and the threshold value K.
  3. Use the Counter class to count the frequency of each element in the test_list.
  4. Use the filter() function with a lambda function to keep only the elements whose frequency is greater than K.
  5. Convert the result into a list and print it

Python3




from collections import Counter
 
test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]
K = 2
freq_dict = Counter(test_list)
res = list(filter(lambda ele: freq_dict[ele] > K, freq_dict))
print("The required elements : " + str(res))
#This code is contributed By Vinay Pinjala.


Output

The required elements : [4, 3]

Time Complexity:

The time complexity of this code is O(n), where n is the length of the input list. This is because the Counter() function from the collections module has a time complexity of O(n), where n is the number of elements in the list. The filter() function and the conversion to a list take linear time, which is also O(n).

Auxiliary Space:

The space complexity of this code is also O(n), where n is the length of the input list. This is because the Counter() function creates a dictionary that stores the frequency count of each element, and the size of this dictionary is proportional to the number of elements in the input list. The output list created by filter() can also be at most n elements long, so the space complexity of the final result is also O(n).

Method #7: Using  defaultdict module from the collections library:

Algorithm:

  1. Import the defaultdict module from the collections library.
  2. Initialize an empty dictionary, freq_dict, using defaultdict(int) to store the frequency count of each element in the test_list.
  3. Set the value of K.
  4. Loop through each key-value pair in freq_dict.
  5. If the frequency of an element is greater than K, append that element to the res list.
  6. Print the final result.
     

Python3




from collections import defaultdict
 
# initializing list
test_list = [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]
  
# printing string
print("The original list : " + str(test_list))
  
freq_dict = defaultdict(int)
for num in test_list:
    freq_dict[num] += 1
K = 2
res = []
for num, freq in freq_dict.items():
    if freq > K:
        res.append(num)
 
# printing results
print("The required elements : " + str(res))
#This code is contributed by Jyothi Pinjala.


Output

The original list : [4, 6, 4, 3, 3, 4, 3, 7, 8, 8]
The required elements : [4, 3]

Time Complexity: O(n), where n is the length of the input list. This is because we iterate through the list once to build the frequency dictionary and once again to find elements with frequency greater than K.

Auxiliary Space: O(n), where n is the length of the input list. This is because we store the frequency counts of each element in the test_list in the freq_dict dictionary. The space taken by the res list is proportional to the number of elements with frequency greater than K. However, the worst-case scenario is when all elements have a unique frequency count, resulting in a dictionary with n key-value pairs.
 



Similar Reads

Python program to print Rows where all its Elements' frequency is greater than K
Given Matrix, extract all rows whose all elements have a frequency greater than K. Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]], K = 2 Output : [[1, 1, 1, 1]] Explanation : Here, frequency of 1 is 4 > 2, hence row retained. Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 3, 4, 1], [4, 5, 6,
8 min read
Python - Extract Dictionary Items with Summation Greater than K
Given dictionary with value lists, extract all those items with values summing over K. Input : {"Gfg" : [6, 3, 4], "is" : [8, 10, 12], "Best" : [10, 16, 14], "for" : [7, 4, 3], "geeks" : [1, 2, 3, 4]}, K = 10 Output : {"Gfg" : [6, 3, 4], "is" : [8, 10, 12], "Best" : [10, 16, 14], "for" : [7, 4, 3], "geeks" : [1, 2, 3, 4]} Explanation : All elements
5 min read
Python - Extract dictionaries with values sum greater than K
Given a dictionaries list, extract all the dictionaries whose keys summation exceeds K. Input : test_list = [{"Gfg" : 4, "is" : 8, "best" : 9}, {"Gfg" : 3, "is": 7, "best" : 5}], K = 15 Output : [{'Gfg': 4, 'is': 8, 'best': 9}] Explanation : 4 + 9 + 8 = 21. Greater than K, hence returned. Input : test_list = [{"Gfg" : 4, "is" : 8, "best" : 9}, {"Gf
8 min read
Python - Extract list with difference in extreme values greater than K
Given a list of lists. The task is to filter all rows whose difference in min and max values is greater than K. Examples: Input : test_list = [[13, 5, 1], [9, 1, 2], [3, 4, 2], [1, 10, 2]], K = 5 Output : [[9, 1, 2], [1, 10, 2], [13, 5, 1]] Explanation : 8, 9, 12 are differences, greater than K. Input : test_list = [[13, 5, 1], [9, 1, 2], [3, 4, 2]
4 min read
Python - Extract element with relative difference greater than K
Given a list of numbers, the task is to write a Python program to extract all numbers with differences of the next and previous elements with a current greater than K. Input : test_list = [2, 7, 4, 1, 9, 2, 3, 10, 1, 5], K = 4Output : [9, 10]Explanation : 9 has 1 as preceding element and 2 as succeeding. 8 and 7 are its difference respectively whic
4 min read
Python - Find the frequency of numbers greater than each element in a list
Given a list, a new list is constructed that has frequency of elements greater than or equal to it, corresponding to each element of the list. Input : test_list = [6, 3, 7, 1, 2, 4] Output : [2, 4, 1, 6, 5, 3] Explanation : 6, 7 are greater or equal to 6 in list, hence 2. Input : test_list = [6, 3, 7] Output : [2, 3, 1] Explanation : 6, 7 are great
8 min read
Sum of all array elements less than X and greater than Y for Q queries
Given a sorted array arr[], and a set Q having M queries, where each query has values X and Y, the task is to find the sum of all integers less than X and greater than Y present in the array. Note: X and Y may or may not be present in the array. Examples: Input: arr[] = [3 5 8 12 15], Q = {{5, 12}, {4, 8}} Output: 18 30 Explanation: For query 1, X
9 min read
Python - Extract elements with equal frequency as value
Given a list, extract all the elements having same frequency as its value. Examples: Input : test_list = [4, 3, 2, 2, 3, 4, 1, 3, 2, 4, 4] Output : [1, 3, 4] Explanation : All elements occur equal times as their value. Input : test_list = [4, 3, 2, 2, 3, 4, 1, 3, 2, 4] Output : [1, 3] Explanation : All elements occur equal times as their value. Met
6 min read
Python Program to Extract Rows of a matrix with Even frequency Elements
Given a Matrix, the task is to write a Python program to extract all the rows which have even frequencies of elements. Examples: Input: [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Output: [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]Explanation: frequency of 4-> 4 which is even frequency of 2-> 2 which is even frequency of 6-> 2 wh
5 min read
Python - Find all pairs of consecutive odd positive integer smaller than a with sum greater than b
Given two positive integers a and b, the task is to write a program in python to find all pairs of consecutive odd numbers which are smaller than the first number a and their sum should be greater than the second number b. Examples: Input: a = 60 b = 100 Output: Pairs of consecutive number are: 51 , 53 53 , 55 55 , 57 57 , 59 Input: a = 20 b = 200
3 min read
Practice Tags :