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’, ‘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 listtest_list = ["Gfg", 'is', 'best', 'for', 'geeks'] # printing original listprint("The original list is : " + str(test_list)) # initializing Lengths list len_list = [3, 2, 4, 3, 5] res = Truefor 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', 'geeks'] Are all strings of required lengths : True
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 listtest_list = ["Gfg", 'is', 'best', 'for', 'geeks'] # printing original listprint("The original list is : " + str(test_list)) # initializing Lengths listlen_list = [3, 2, 4, 3, 5] # all() used to check for each element for lengthres = all(len(test_list[idx]) == len_list[idx] for idx in range(len(test_list))) # printing resultprint("Are all strings of required lengths : " + str(res)) |
The original list is : ['Gfg', 'is', 'best', 'for', 'geeks'] Are all strings of required lengths : True
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


