Python – Remove Non-English characters Strings from List
Given a List of Strings, perform removal of all Strings with non-english characters.
Input : test_list = [‘Good| ????’, ‘??Geeks???’] Output : [] Explanation : Both contain non-English characters Input : test_list = [“Gfg”, “Best”] Output : [“Gfg”, “Best”] Explanation : Both are valid English words.
Method #1 : Using regex + findall() + list comprehension
In this, we create a regex of unicodes and check for occurrence in String List, extract each String without unicode using findall().
Python3
# Python3 code to demonstrate working of# Remove Non-English characters Strings from List# Using regex + findall() + list comprehensionimport re# initializing listtest_list = ['Gfg', 'Good| ????', "for", '??Geeks???']# printing original listprint("The original list is : " + str(test_list))# using findall() to neglect unicode of Non-English alphabetsres = [idx for idx in test_list if not re.findall("[^\u0000-\u05C0\u2100-\u214F]+", idx)]# printing resultprint("The extracted list : " + str(res)) |
Method #2 : Using regex + search() + filter() + lambda
In this, we search for only English alphabets in String, and extract only those that have those. We use filter() + lambda to perform the task of passing filter functionality and iteration.
Python3
# Python3 code to demonstrate working of# Remove Non-English characters Strings from List# Using regex + search() + filter() + lambdaimport re# initializing listtest_list = ['Gfg', 'Good| ????', "for", '??Geeks???']# printing original listprint("The original list is : " + str(test_list))# using search() to get only those strings with alphabetsres = list(filter(lambda ele: re.search("[a-zA-Z\s]+", ele) is not None, test_list))# printing resultprint("The extracted list : " + str(res)) |
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3 : Using for loop
Python3
# Python3 code to demonstrate working of# Remove Non-English characters Strings from List# initializing listtest_list = ['Gfg', 'Good| ????', "for", '??Geeks???']# printing original listprint("The original list is : " + str(test_list))loweralphabets="abcdefghijklmnopqrstuvwxyz"upperalphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZ"x=loweralphabets+upperalphabetsres=[]for i in test_list: a="" for j in i: if j in x: a+=j res.append(a) # printing resultprint("The extracted list : " + str(res)) |
The original list is : ['Gfg', 'Good| ????', 'for', '??Geeks???'] The extracted list : ['Gfg', 'Good', 'for', 'Geeks']





Please Login to comment...