Python – Strings with Maximum K length

Sometimes, while working with huge amount of data, we can have a problem in which we need to extract just specific sized strings which don’t exceed a specific length. 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 strings which have length smaller than K checked using len().

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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

 

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to demonstrate working of
# Extract Strings with Maximum K length
# using filter() + lambd
  
# initialize list 
test_list = ['gfg', 'is', 'best', 'for', 'geeks']
  
# printing original list 
print("The original list : " + str(test_list))
  
# initialize K 
K = 3
  
# Extract Strings with Maximum K length
# using filter() + lambda
res = list(filter(lambda ele: len(ele) <= K, test_list))
  
# printing result
print("The maximum K sized strings are : " + str(res))

chevron_right


Output :

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.