Open In App

Python – Filter Strings within ASCII range

Last Updated : 08 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Given ASCII or alphabetical range, filter strings are found in a particular range.

Input : test_list = [“gfg”, “is”, “best”, “for”, “geeks”], strt_asc, end_asc = 105, 115 
Output : [‘is’] 
Explanation : i has 105, and s has 115, which is in range ASCII values.
Input : test_list = [“gfg”, “is”, “best”, “for”, “geeks”], strt_asc, end_asc = 100, 115 
Output : [‘gfg’, ‘is’, ‘for’, ‘geeks’] 
Explanation : Strings with range characters included. 
 

Method #1 : Using list comprehension + all() + ord()

In this, we check for all characters to be in the given ASCII range, computed using ord(), and accordingly, strings are filtered.

Python3




# Python3 code to demonstrate working of
# Filter Strings within ASCII range
# Using list comprehension + ord() + all()
 
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing ASCII range
strt_asc, end_asc = 105, 115
 
# checking for all characters to be in ASCII range
res = [sub for sub in test_list if all(
    ord(ele) >= strt_asc and ord(ele) <= end_asc for ele in sub)]
 
# printing result
print("Filtered Strings : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
Filtered Strings : ['is']

Time Complexity: O(n) where n is the number of elements in the list “test_list”.  
Auxiliary Space: O(1), no extra space is required

Method #2 : Using filter() + lambda + all() + ord()

In this, we perform task of filtering using filter() and lambda function, ord() and all() is used in similar way as above method.

Python3




# Python3 code to demonstrate working of
# Filter Strings within ASCII range
# Using filter() + lambda + all() + ord()
 
# initializing list
test_list = ["gfg", "is", "best", "for", "geeks"]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing ASCII range
strt_asc, end_asc = 105, 115
 
# checking for all characters to be in ASCII range
res = list(filter(lambda sub: all(ord(ele) >= strt_asc and ord(
    ele) <= end_asc for ele in sub), test_list))
 
# printing result
print("Filtered Strings : " + str(res))


Output

The original list is : ['gfg', 'is', 'best', 'for', 'geeks']
Filtered Strings : ['is']

Time Complexity: O(n*n)
Auxiliary Space: O(1)

METHOD 3:Using for

APPROACH:

This approach uses a for loop to iterate through each string in the given list and checks if each character in the string falls within the given ASCII range. If all characters in the string fall within the range, then the string is added to a new list.

ALGORITHM:

1.Initialize test_list, strt_asc, and end_asc with the given values.
2.Initialize an empty list result.
3.Iterate through each string in test_list.
4.For each string, iterate through each character in the string.
5.Check if the ASCII value of the character falls within the given range.
6.If all characters in the string fall within the range, add the string to the result list.
7.Print the result list.

Python3




test_list = ["gfg", "is", "best", "for", "geeks"]
strt_asc, end_asc = 105, 115
 
result = []
for s in test_list:
    flag = True
    for c in s:
        if ord(c) < strt_asc or ord(c) > end_asc:
            flag = False
            break
    if flag:
        result.append(s)
 
print(result)


Output

['is']

Time complexity: O(n * k) where n is the length of the given list and k is the length of the longest string in the list. This is because we need to iterate through each character in each string in the list.

Space complexity: O(n) since we are storing the filtered list in memory. The space required for strt_asc and end_asc is negligible.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads