Python – Strings with Maximum K length
Last Updated :
03 May, 2023
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
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
print ( "The original list : " + str (test_list))
K = 3
res = [ele for ele in test_list if len (ele) < = K]
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
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
print ( "The original list : " + str (test_list))
K = 3
res = list ( filter ( lambda ele: len (ele) < = K, test_list))
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
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
K = 3
res = []
for ele in test_list:
if len (ele) < = K:
res.append(ele)
print ( "The maximum K sized strings are : " + str (res))
|
Output
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
test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ]
K = 3
gen_exp = (ele for ele in test_list if len (ele) < = K)
res = list (gen_exp)
print ( "The maximum K sized strings are: " + str (res))
|
Output
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.
Share your thoughts in the comments
Please Login to comment...