Sometimes, while working with huge amount of data, we can have a problem in which we need to extract just specific range sized strings. 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 ranged sized strings checked using len().
# Python3 code to demonstrate working of # Range length Strings extraction # using list comprehension + len() # initialize list test_list = ['gfg', 'is', 'best', 'for', 'geeks'] # printing original list print("The original list : " + str(test_list)) # initialize i, j i, j = 2, 3 # Range length Strings extraction # using list comprehension + len() res = [ele for ele in test_list if len(ele) >= i and len(ele) <= j] # printing result print("The range sized strings are : " + str(res)) |
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] The range 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.
# Python3 code to demonstrate working of # Range length Strings extraction # using filter() + lambd # initialize list test_list = ['gfg', 'is', 'best', 'for', 'geeks'] # printing original list print("The original list : " + str(test_list)) # initialize i, j i, j = 2, 3 # Range length Strings extraction # using filter() + lambda res = list(filter(lambda ele: len(ele) >= i and len(ele) <= j, test_list)) # printing result print("The range sized strings are : " + str(res)) |
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] The range sized strings are : ['gfg', 'is', 'for']
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.
Recommended Posts:
- Python | Extract K sized strings
- Python - Incremental K sized Row Matrix Initialization
- Python | Zip different sized list
- Python | Column summation in uneven sized lists
- Python | Uneven Sized Matrix Column product
- Python - Uneven Sized Matrix Column Minimum
- Python - Rear column in Multi-sized Matrix
- Python - Convert dictionary to K sized dictionaries
- Python program to convert String to K sized Numerical Rows
- Python - N sized substrings with K distinct characters
- Python - Sort Matrix by K Sized Subarray Maximum Sum
- PyQt5 - Different sized padding Progress Bar
- Python - Extract elements with Range consecutive occurrences
- Python - Extract range characters from String
- Python - Extract tuples with elements in Range
- Python - Extract range of Consecutive Similar elements ranges from string list
- Python program to extract characters in given range from a string list
- Python | Extract numbers from list of strings
- Python | Extract Strings with only Alphabets
- Python | Extract Nth words in Strings List
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.

