Open In App

Python – Filter above Threshold size Strings

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, while working with huge amounts of data, we can have a problem in which we need to extract just specific-sized strings above a minimum threshold. 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 the above functionalities can be used to perform this task. In this, we iterate for all the strings and return only above threshold strings checked using len() function. 

Python3




# Python3 code to demonstrate working of
# Filter above Threshold size Strings
# using list comprehension + len()
 
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# Printing original list
print("The original list : " + str(test_list))
 
# Initialize Threshold
thres = 4
 
# Filter above Threshold size Strings
# using list comprehension + len()
res = [ele for ele in test_list if len(ele) >= thres]
 
# Printing result
print("The above Threshold size strings are : " + str(res))


Output : 

The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The above Threshold size strings are : ['best', 'geeks']

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

Method #2: Using filter() + lambda 

The combination of the 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. 

Python3




# Python3 code to demonstrate working of
# Filter above Threshold size Strings
# using filter() + lambda
 
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# Printing original list
print("The original list : " + str(test_list))
 
# Initialize Threshold
thres = 4
 
# Filter above Threshold size Strings
# using filter() + lambda
res = list(filter(lambda ele: len(ele) >= thres, test_list))
 
# Printing result
print("The above Threshold size strings are : " + str(res))


Output : 

The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The above Threshold size strings are : ['best', 'geeks']

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

Method #3: Using copy() + remove() + join() methods

Python3




# Python3 code to demonstrate working of
# Filter above Threshold size Strings
 
# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize Threshold
thres = 4
 
for i in test_list.copy():
    if len(i) < thres:
        test_list.remove(i)
# printing result
print("The above Threshold size strings are : " + ' '.join(test_list))


Output

The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The above Threshold size strings are : best geeks

Time Complexity: O(n) where n is the number of elements in the list “test_list”.copy() + remove() + join() methods performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list

Method #4: Using numpy.array() and numpy.char.str_len()

Note: Install numpy module using command “pip install numpy”

Python3




import numpy as np
 
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# Printing original list
print("The original list : " + str(test_list))
 
# Initialize Threshold
thres = 4
 
# Converting list to numpy array
test_array = np.array(test_list)
 
# Getting the length of each string in the array
# using numpy.char.str_len() function
lengths = np.char.str_len(test_array)
 
# using numpy array indexing to filter above threshold strings
res = test_array[lengths >= thres]
 
# printing result
print("The above Threshold size strings are : " + str(res))
 
# This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The above Threshold size strings are : best geeks

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

In this method, we first convert the input list to a numpy array using numpy.array(). Then we use numpy.char.str_len() to get the length of each string in the array. Finally, we use numpy array indexing to filter the strings with lengths greater than or equal to the threshold. This method uses numpy’s built-in functions and can be more efficient for large input lists.

Method 5: using a for loop and a conditional statement

Steps:

  1. Initialize a list test_list with some strings.
  2. Print the original list using print() function.
  3. Initialize a variable thres with some threshold value.
  4. Initialize an empty list res.
  5. Use a for loop to iterate over each element in test_list.
  6. Use a conditional statement to check if the length of the current element is greater than or equal to the threshold value.
  7. If the condition is true, append the current element to the res list.
  8. After the loop, print the filtered list using print() function.

Python3




# Python3 code to demonstrate working of
# Filter above Threshold size Strings
# using for loop and conditional statement
 
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# Printing original list
print("The original list : " + str(test_list))
 
# Initialize Threshold
thres = 4
 
# Filter above Threshold size Strings
# using for loop and conditional statement
res = []
for ele in test_list:
    if len(ele) >= thres:
        res.append(ele)
 
# Printing result
print("The above Threshold size strings are : " + str(res))


Output

The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The above Threshold size strings are : ['best', 'geeks']

Time complexity: O(N), where n is the length of the list.
Auxiliary space: O(K), where k is the number of elements that satisfy the condition.



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