Python – Test for desired String Lengths
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’, ‘Beginner’], 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
# Python3 code to demonstrate working of # Test for desired String Lengths # Using loop # initializing string list test_list = [ "Gfg" , 'is' , 'best' , 'for' , 'Beginner' ] # printing original list print ( "The original list is : " + str (test_list)) # initializing Lengths list len_list = [ 3 , 2 , 4 , 3 , 5 ] res = True for idx in range ( len (test_list)): # checking for string lengths if len (test_list[idx]) ! = len_list[idx]: res = False break # printing result print ( "Are all strings of required lengths : " + str (res)) |
The original list is : ['Gfg', 'is', 'best', 'for', 'Beginner'] 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
# Python3 code to demonstrate working of # Test for desired String Lengths # Using all() # initializing string list test_list = [ "Gfg" , 'is' , 'best' , 'for' , 'Beginner' ] # printing original list print ( "The original list is : " + str (test_list)) # initializing Lengths list len_list = [ 3 , 2 , 4 , 3 , 5 ] # all() used to check for each element for length res = all ( len (test_list[idx]) = = len_list[idx] for idx in range ( len (test_list))) # printing result print ( "Are all strings of required lengths : " + str (res)) |
The original list is : ['Gfg', 'is', 'best', 'for', 'Beginner'] 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
# Python3 code to demonstrate working of # Test for desired String Lengths # Using List Comprehension # initializing string list test_list = [ "Gfg" , 'is' , 'best' , 'for' , 'Beginner' ] # printing original list print ( "The original list is : " + str (test_list)) # initializing Lengths list len_list = [ 3 , 2 , 4 , 3 , 5 ] # using list comprehension result = all ([ len (test_list[idx]) = = len_list[idx] for idx in range ( len (test_list))]) # printing result print ( "Are all strings of required lengths : " + str (result)) |
The original list is : ['Gfg', 'is', 'best', 'for', 'Beginner'] 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
# Python3 code to demonstrate working of # Test for desired String Lengths # Using map() and lambda function # initializing string list test_list = [ "Gfg" , 'is' , 'best' , 'for' , 'Beginner' ] # printing original list print ( "The original list is : " + str (test_list)) # initializing Lengths list len_list = [ 3 , 2 , 4 , 3 , 5 ] # using map() and lambda function to check for length res = all ( map ( lambda x, y: len (x) = = y, test_list, len_list)) # printing result print ( "Are all strings of required lengths : " + str (res)) |
The original list is : ['Gfg', 'is', 'best', 'for', 'Beginner'] 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
# Initialize the string list test_list = [ "Gfg" , 'is' , 'best' , 'for' , 'Beginner' ] # Initialize the required string lengths len_list = [ 3 , 2 , 4 , 3 , 5 ] # Use filter and lambda function to create a new list with only tuples of matching string length and required length matching_lengths = filter ( lambda x: len (x[ 0 ]) = = x[ 1 ], zip (test_list, len_list)) # Use len to count the number of tuples with matching lengths count_matching_lengths = len ( list (matching_lengths)) # Check if the number of tuples with matching lengths is equal to the length of the original list res = count_matching_lengths = = len (test_list) # Print the result print ( "Are all strings of required lengths: " + str (res)) |
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.