Python – Test for desired String Lengths
Last Updated :
08 May, 2023
Given a String list, check for all string if it matches the desired string length from 2nd list of sizes.
Input : test_list = [“Gfg”, ‘for’, ‘geeks’], len_list = [3, 3, 5]
Output : True
Explanation : All are of desired lengths.
Input : test_list = [“Gfg”, ‘for’, ‘geek’], len_list = [3, 3, 5]
Output : False
Explanation : geek has len 4, but desired is 5.
Method #1: Using loop
In this, we iterate for all the strings, and flag results false if we get any string not matching the required size.
Python3
test_list = [ "Gfg" , 'is' , 'best' , 'for' , 'geeks' ]
print ( "The original list is : " + str (test_list))
len_list = [ 3 , 2 , 4 , 3 , 5 ]
res = True
for idx in range ( len (test_list)):
if len (test_list[idx]) ! = len_list[idx]:
res = False
break
print ( "Are all strings of required lengths : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
Are all strings of required lengths : True
Time Complexity: O(n)
Space Complexity: O(n)
Method #2 : Using all()
This returns True if all lengths match to be equal to desired lengths from other lists.
Python3
test_list = [ "Gfg" , 'is' , 'best' , 'for' , 'geeks' ]
print ( "The original list is : " + str (test_list))
len_list = [ 3 , 2 , 4 , 3 , 5 ]
res = all ( len (test_list[idx]) = = len_list[idx]
for idx in range ( len (test_list)))
print ( "Are all strings of required lengths : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
Are all strings of required lengths : True
Time Complexity: O(n)
Space Complexity: O(n)
Method #3: Using List Comprehension
In this approach, we can use a list comprehension that returns a list of True or False based on whether the lengths of each string match the desired length. The result is then passed to all method, which will return True if all the elements are True, otherwise False.
Python3
test_list = [ "Gfg" , 'is' , 'best' , 'for' , 'geeks' ]
print ( "The original list is : " + str (test_list))
len_list = [ 3 , 2 , 4 , 3 , 5 ]
result = all ([ len (test_list[idx]) = = len_list[idx] for idx in range ( len (test_list))])
print ( "Are all strings of required lengths : " + str (result))
|
Output
The original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
Are all strings of required lengths : True
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4: using the map() function along with lambda function.
steps to implement this method:
- Initialize the list of strings and the list of required lengths, same as the previous method.
- Define a lambda function that takes two arguments, a string and an integer, and returns True if the length of the string is equal to the integer, False otherwise.
- Use the map() function to apply the lambda function to each string in the list and its corresponding required length in the length list.
- Convert the resulting map object to a list and use the all() function to check if all elements in the list are True.
- Print the result.
Python3
test_list = [ "Gfg" , 'is' , 'best' , 'for' , 'geeks' ]
print ( "The original list is : " + str (test_list))
len_list = [ 3 , 2 , 4 , 3 , 5 ]
res = all ( map ( lambda x, y: len (x) = = y, test_list, len_list))
print ( "Are all strings of required lengths : " + str (res))
|
Output
The original list is : ['Gfg', 'is', 'best', 'for', 'geeks']
Are all strings of required lengths : True
Time complexity: O(n), where n is the length of the string list.
Auxiliary space: O(1).
Method 5: Using filter(), lambda and len()
Step-by-step algorithm:
- Initialize the string list and the required string lengths list
- Use filter() function with a lambda function to filter the elements of the string list and the required string lengths list based on the condition
- that the length of the string is equal to its required length.
- Use the zip() function to generate a list of tuples that contains the corresponding elements from the two filtered lists.
- Use len() function with the filtered list of tuples to count the number of tuples where the string length matches its required length.
- Compare the length of the filtered list with the length of the original string list to determine if all the strings are of required length.
- Print the result.
Python3
test_list = [ "Gfg" , 'is' , 'best' , 'for' , 'geeks' ]
len_list = [ 3 , 2 , 4 , 3 , 5 ]
matching_lengths = filter ( lambda x: len (x[ 0 ]) = = x[ 1 ], zip (test_list, len_list))
count_matching_lengths = len ( list (matching_lengths))
res = count_matching_lengths = = len (test_list)
print ( "Are all strings of required lengths: " + str (res))
|
Output
Are all strings of required lengths: True
Time complexity:
The time complexity of the given approach is O(n), where n is the length of the string list. This is because we are iterating through the list once to filter and create a new list of tuples, and then we are iterating through this new list once to count the tuples where the string length matches its required length.
Auxiliary space complexity:
The auxiliary space complexity of the given approach is O(k), where k is the number of tuples where the string length matches its required length. This is because we are creating a new list of tuples to store the filtered elements, and the size of this list depends on the number of tuples where the string length matches its required length.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...