Open In App

Python | Extract K sized strings

Improve
Improve
Like Article
Like
Save
Share
Report

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

Python3




# Python3 code to demonstrate working of
# Extract K sized 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 K
K = 3
 
# Extract K sized strings
# using list comprehension + len()
res = [ele for ele in test_list if len(ele) == K]
 
# printing result
print("The K sized strings are : " + str(res))


Output : 

The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The K sized strings are : ['gfg', 'for']

 

Time Complexity: O(n), where n is the length of the input list. This is because we’re using list comprehension + len() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.

Method #2 : Using filter() + lambda 
The combination of 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 K sized strings
# 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 K sized strings
# using filter() + lambda
res = list(filter(lambda ele: len(ele) == K, test_list))
 
# printing result
print("The K sized strings are : " + str(res))


Output : 

The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The K sized strings are : ['gfg', 'for']

 

Time complexity: O(n), where n is the length of the numbers list. The filter() + lambda function has a constant time complexity of O(n)

Auxiliary Space: O(n), as we create new list res where n is the length of the numbers list.

Method #3: Using map() function and lambda expression

Algorithm:

  1. Initialize the list and K
  2. Apply filter() and lambda expression to filter out the strings with length equal to K
  3. Apply map() and lambda expression to get the filtered strings
  4. Convert the result to a list
  5. Print the output

Below is the implementation of the above approach:

Python3




# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize K
K = 3
 
# Extract K sized strings
# using map() function and lambda expression
res = list(map(lambda x:x, filter(lambda x: len(x)==K, test_list)))
 
# printing result
print("The K sized strings are : " + str(res))


Output

The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The K sized strings are : ['gfg', 'for']

Time complexity:
The time complexity of filter() function is O(N) where N is the length of the list. The lambda function inside filter() function has a time complexity of O(1). The map() function also has a time complexity of O(N). The lambda function inside map() function has a time complexity of O(1). Therefore, the overall time complexity of the code is O(N).

Auxiliary space:
The space complexity of filter() function and map() function is O(1) as they are not creating any additional space. Therefore, the overall space complexity of the code is also O(N).

Method #4: Using a for loop

  • Initialize an empty list res to store the K sized strings.
  • Loop through each element ele in the input list test_list.
  • Check if the length of ele is equal to K. If yes, append ele to res.
  • Once all elements have been checked, return res.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Extract K sized strings
# using a for loop
 
# initialize list
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
 
# printing original list
print("The original list : " + str(test_list))
 
# initialize K
K = 3
 
# Extract K sized strings
# using a for loop
res = []
for ele in test_list:
    if len(ele) == K:
        res.append(ele)
 
# printing result
print("The K sized strings are : " + str(res))


Output

The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The K sized strings are : ['gfg', 'for']

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(k), where k is the number of strings of length K in the input list (worst case is when all strings have length K and thus the new list will have k elements).



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