Open In App

Python – Test for desired String Lengths

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Test for desired String Lengths
# Using loop
 
# initializing string list
test_list = ["Gfg", 'is', 'best', 'for', 'geeks']
 
# 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))


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




# Python3 code to demonstrate working of
# Test for desired String Lengths
# Using all()
 
# initializing string list
test_list = ["Gfg", 'is', 'best', 'for', 'geeks']
 
# 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))


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




# Python3 code to demonstrate working of
# Test for desired String Lengths
# Using List Comprehension
  
# initializing string list
test_list = ["Gfg", 'is', 'best', 'for', 'geeks']
  
# 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))


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:

  1. Initialize the list of strings and the list of required lengths, same as the previous method.
  2. 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.
  3. Use the map() function to apply the lambda function to each string in the list and its corresponding required length in the length list.
  4. Convert the resulting map object to a list and use the all() function to check if all elements in the list are True.
  5. 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', 'geeks']
 
# 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))


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:

  1. Initialize the string list and the required string lengths list
  2. 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
  3. that the length of the string is equal to its required length.
  4. Use the zip() function to generate a list of tuples that contains the corresponding elements from the two filtered lists.
  5. Use len() function with the filtered list of tuples to count the number of tuples where the string length matches its required length.
  6. 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.
  7. Print the result.

Python3




# Initialize the string list
test_list = ["Gfg", 'is', 'best', 'for', 'geeks']
 
# 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))


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
Previous
Next
Share your thoughts in the comments

Similar Reads