Friday, October 10, 2025
HomeLanguagesPython | Threshold Size Greater Strings Frequency

Python | Threshold Size Greater Strings Frequency

Sometimes, while working with huge amount of data, we can have a problem in which we need to know count of just specific sized strings which are greater than a specific length. This kind of problem can occur during validation cases across many domains. Let’s discuss certain ways to handle this in Python strings list.

Method #1 : Using list comprehension + len() 
The combination of above functionalities can be used to perform this task. In this, we iterate for all the strings and return only strings which have length greater than K checked using len(). The count is extracted using len().

Python3




# Python3 code to demonstrate working of
# Threshold Size Greater Strings Frequency
# using list comprehension + len()
 
# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'neveropen']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize K
K = 3
 
# Threshold Size Greater Strings Frequency
# using list comprehension + len()
res = len([ele for ele in test_list if len(ele) >= K])
 
# printing result
print("The frequency of threshold K sized strings are : " + str(res))


Output

The original list : ['gfg', 'is', 'best', 'for', 'neveropen']
The frequency of threshold K sized strings are : 4

 
Method #2 : Using filter() + len() + lambda 
The combination of above functionalities can be used to perform this task. In this, we extract the elements using filter() and logic is compiled in a lambda function. The count is extracted using len().

Python3




# Python3 code to demonstrate working of
# Threshold Size Greater Strings Frequency
# using filter() + lambda + len()
 
# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'neveropen']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize K
K = 3
 
# Threshold Size Greater Strings Frequency
# using filter() + lambda + len()
res = len(list(filter(lambda ele: len(ele) >= K, test_list)))
 
# printing result
print("The frequency of threshold K sized strings are : " + str(res))


Output

The original list : ['gfg', 'is', 'best', 'for', 'neveropen']
The frequency of threshold K sized strings are : 4

Method #3: Using a for loop

  • Initialize a counter variable to 0.
  • Loop through each element in the list using a for loop.
  • For each element, check if its length is greater than or equal to K.
  • If it is, increment the counter variable.
  • After the loop finishes, the value of the counter variable is the frequency of threshold K sized strings.
  • Print the frequency.

Python3




test_list = ['gfg', 'is', 'best', 'for', 'neveropen']
 
print("The original list : " + str(test_list))
 
K = 3
 
count = 0
for ele in test_list:
    if len(ele) >= K:
        count += 1
 
print("The frequency of threshold K sized strings are : " + str(count))


Output

The original list : ['gfg', 'is', 'best', 'for', 'neveropen']
The frequency of threshold K sized strings are : 4

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(1), since we only use a counter variable.

Method 4: Using numpy

Python3




# Python3 code to demonstrate working of
# Threshold Size Greater Strings Frequency
# using numpy
 
import numpy as np
 
# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'neveropen']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize K
K = 3
 
# Threshold Size Greater Strings Frequency
# using numpy
res = np.sum(np.array([len(ele) >= K for ele in test_list]))
 
# printing result
print("The frequency of threshold K sized strings are : " + str(res))


Output:

The original list : ['gfg', 'is', 'best', 'for', 'neveropen']
The frequency of threshold K sized strings are : 4

Time complexity: O(n), where n is the length of the input list. 
Auxiliary space: O(n) auxiliary space to create the intermediate list of Boolean values.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS