Python - Strings with Maximum K length
Last Updated :
12 Jul, 2025
Sometimes, while working with huge amounts of data, we can have a problem in which we need to extract just specific-sized strings which don't exceed a specific length. This kind of problem can occur during validation cases across many domains. Let’s discuss certain ways to handle this in a Python string 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 strings that have lengths smaller than K checked using len() function.
Python3
# Python3 code to demonstrate working of
# Extract Strings with Maximum K length
# using list comprehension + len()
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
# printing original list
print("The original list : " + str(test_list))
# initialize K
K = 3
# Extract Strings with Maximum K length
# using list comprehension + len()
res = [ele for ele in test_list if len(ele) <= K]
# Printing result
print("The maximum K sized strings are : " + str(res))
Output : The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The maximum K sized strings are : ['gfg', 'is', 'for']
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 list “test_list”.
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
# Extract Strings with Maximum K length
# using filter() + lambda
# Initialize list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
# Printing original list
print("The original list : " + str(test_list))
# initialize K
K = 3
# Extract Strings with Maximum K length
# using filter() + lambda
res = list(filter(lambda ele: len(ele) <= K, test_list))
# Printing result
print("The maximum K sized strings are : " + str(res))
Output : The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The maximum K sized strings are : ['gfg', 'is', 'for']
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 list “test_list”.
Method#3:Using a for loop
Algorithm:
- Initialize the given list of strings.
- Initialize the maximum length of the strings that should be extracted to a variable K.
- Initialize an empty list to store the strings that have a length less than or equal to K.
- Iterate through each element of the given list of strings.
- Check if the length of the element is less than or equal to K.
- If the length of the element is less than or equal to K, add the element to the list of strings with a length less than or equal to K.
- Return the list of strings with a length less than or equal to K.
Python3
# Initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
# Initialize K
K = 3
# Initialize result list
res = []
# Iterating through each element in the list
for ele in test_list:
# Checking if the length of the element is less than or equal to K
if len(ele) <= K:
# If so, add the element to the result list
res.append(ele)
# Print the result list
print("The maximum K sized strings are : " + str(res))
OutputThe maximum K sized strings are : ['gfg', 'is', 'for']
Time Complexity:
- In the worst case, the algorithm iterates through each element of the given list of strings once.
- The time taken to iterate through each element of the list is O(N), where N is the length of the list.
- The time taken to check the length of each element and append it to the result list is O(1) in the average case.
- Therefore, the overall time complexity of the algorithm is O(N), where N is the length of the list.
Auxiliary Space:
- The algorithm initializes an empty list to store the strings with a length less than or equal to K.
- The size of this list depends on the number of strings in the original list that have a length less than or equal to K.
- Therefore, the auxiliary space complexity of the algorithm is O(M), where M is the number of strings in the original list that have a length less than or equal to K.
Method 4: Using Generator Expressions
Generators are written just like a normal function but we use yield() instead of return() for returning a result.
Steps:
- Initialize the list 'test_list' and the integer K.
- Define a generator expression that yields the elements in test_list that have a length less than or equal to K.
- Convert the generator expression into a list.
- Print the resulting list.
Python3
# Initializing list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
# Initializing K
K = 3
# Defining generator expression
gen_exp = (ele for ele in test_list if len(ele) <= K)
# Converting generator expression to list
res = list(gen_exp)
# Printing resultant list
print("The maximum K sized strings are: " + str(res))
OutputThe maximum K sized strings are: ['gfg', 'is', 'for']
Time complexity: O(N), where n is the length of test_list.
Auxiliary Space: O(1), as we are not creating any additional data structures other than the resulting list.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice