Python - Extract range sized strings
Last Updated :
13 Apr, 2023
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
# 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))
Output : The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The range sized strings are : ['gfg', 'is', 'for']
Time Complexity: O(n) where n is the number of elements in the string list. The list comprehension + len() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list.
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
# Python3 code to demonstrate working of
# Range length Strings extraction
# using filter() + lambda
# 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))
Output : The original list : ['gfg', 'is', 'best', 'for', 'geeks']
The range sized strings are : ['gfg', 'is', 'for']
Time Complexity: O(n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method #3: Using a for loop and if statement
- Initialize an empty list to store the range-sized strings.
- Iterate over each string in the input list using a for loop.
- Check if the length of the current string is greater than or equal to i and less than or equal to j using an if statement.
- If the condition is true, append the current string to the result list.
- Return the result list.
Python3
# Python3 code to demonstrate working of
# Range length Strings extraction
# using for loop and if statement
# 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 for loop and if statement
res = []
for ele in test_list:
if len(ele) >= i and len(ele) <= j:
res.append(ele)
# printing result
print("The range sized strings are : " + str(res))
OutputThe original list : ['gfg', 'is', 'best', 'for', 'geeks']
The range sized strings are : ['gfg', 'is', 'for']
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(m), where m is the number of range sized strings found in the input list.
Similar Reads
Python | Extract K sized strings Sometimes, while working with huge amount of data, we can have a problem in which we need to extract just specific 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 compr
5 min read
Python Extract Substring Using Regex Python provides a powerful and flexible module called re for working with regular expressions. Regular expressions (regex) are a sequence of characters that define a search pattern, and they can be incredibly useful for extracting substrings from strings. In this article, we'll explore four simple a
2 min read
Python - Extract range characters from String Given a String, extract characters only which lie between given letters. Input : test_str = 'geekforgeeks is best', strt, end = "g", "s" Output : gkorgksiss Explanation : All characters after g and before s are retained. Input : test_str = 'geekforgeeks is best', strt, end = "g", "r" Output : gkorgk
4 min read
Python - Extract string between two substrings The problem is to extract the portion of a string that lies between two specified substrings. For example, in the string "Hello [World]!", if the substrings are "[" and "]", the goal is to extract "World".If the starting or ending substring is missing, handle the case appropriately (e.g., return an
3 min read
Python | Convert String ranges to list Sometimes, while working in applications we can have a problem in which we are given a naive string that provides ranges separated by a hyphen and other numbers separated by commas. This problem can occur across many places. Let's discuss certain ways in which this problem can be solved. Method #1:
6 min read
Python - Extract K length substrings The task is to extract all possible substrings of a specific length, k. This problem involves identifying and retrieving those substrings in an efficient way. Let's explore various methods to extract substrings of length k from a given string in PythonUsing List Comprehension List comprehension is t
2 min read
Size of String in Memory - Python There can be situations in which we require to get the size that the string takes in bytes using Python which is usually useful in case one is working with files. Let's discuss certain ways in which this can be performed.Using sys.getsizeof()This task can also be performed by one of the system calls
2 min read
Python | Reverse Interval Slicing String Sometimes, while working with strings, we can have a problem in which we need to perform string slicing. In this, we can have a variant in which we need to perform reverse slicing that too interval. This kind of application can come in day-day programming. Let us discuss certain ways in which this t
4 min read
Slicing range() function in Python range() allows users to generate a series of numbers within a given range. Depending on how many arguments the user is passing to the function, the user can decide where that series of numbers will begin and end as well as how big the difference will be between one number and the next.range() takes
2 min read
Python | Split by repeating substring Sometimes, while working with Python strings, we can have a problem in which we need to perform splitting. This can be of a custom nature. In this, we can have a split in which we need to split by all the repetitions. This can have applications in many domains. Let us discuss certain ways in which t
5 min read