Python – Strings with Maximum K length

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 listtest_list = ['gfg', 'is', 'best', 'for', 'zambiatek']# printing original listprint("The original list : " + str(test_list))# initialize KK = 3# Extract Strings with Maximum K length# using list comprehension + len()res = [ele for ele in test_list if len(ele) <= K]# Printing resultprint("The maximum K sized strings are : " + str(res)) |
The original list : ['gfg', 'is', 'best', 'for', 'zambiatek'] 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 listtest_list = ['gfg', 'is', 'best', 'for', 'zambiatek']# Printing original listprint("The original list : " + str(test_list))# initialize KK = 3# Extract Strings with Maximum K length# using filter() + lambdares = list(filter(lambda ele: len(ele) <= K, test_list))# Printing resultprint("The maximum K sized strings are : " + str(res)) |
The original list : ['gfg', 'is', 'best', 'for', 'zambiatek'] 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 listtest_list = ['gfg', 'is', 'best', 'for', 'zambiatek']# Initialize KK = 3# Initialize result listres = []# Iterating through each element in the listfor 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 listprint("The maximum K sized strings are : " + str(res)) |
The 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 listtest_list = ['gfg', 'is', 'best', 'for', 'zambiatek']# Initializing KK = 3# Defining generator expressiongen_exp = (ele for ele in test_list if len(ele) <= K)# Converting generator expression to listres = list(gen_exp)# Printing resultant listprint("The maximum K sized strings are: " + str(res)) |
The 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.



