Open In App

Python – Strings with Maximum K length

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 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:

  1. Initialize the given list of strings.
  2. Initialize the maximum length of the strings that should be extracted to a variable K.
  3. Initialize an empty list to store the strings that have a length less than or equal to K.
  4. Iterate through each element of the given list of strings.
  5. Check if the length of the element is less than or equal to K.
  6. 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.
  7. 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))


Output

The maximum K sized strings are : ['gfg', 'is', 'for']

Time Complexity:

  1. In the worst case, the algorithm iterates through each element of the given list of strings once.
  2. The time taken to iterate through each element of the list is O(N), where N is the length of the list.
  3. The time taken to check the length of each element and append it to the result list is O(1) in the average case.
  4. Therefore, the overall time complexity of the algorithm is O(N), where N is the length of the list.

Auxiliary Space:

  1. The algorithm initializes an empty list to store the strings with a length less than or equal to K.
  2. 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.
  3. 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:

  1. Initialize the list ‘test_list’ and the integer K.
  2. Define a generator expression that yields the elements in test_list that have a length less than or equal to K.
  3. Convert the generator expression into a list.
  4. 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))


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.



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