Python - Filter rows with only Alphabets from List of Lists
Last Updated :
26 Apr, 2023
Given Matrix, write a Python program to extract rows which only contains alphabets in its strings.
Examples:
Input : test_list = [["gfg", "is", "best"], ["Geeks4Geeks", "good"], ["Gfg is good"], ["love", "gfg"]]
Output : [['gfg', 'is', 'best'], ['love', 'gfg']]
Explanation : All strings with just alphabets are extracted.
Input : test_list = [["gfg", "is", "!best"], ["Geeks4Geeks", "good"], ["Gfg is good"], ["love", "gfg"]]
Output : [['love', 'gfg']]
Explanation : All strings with just alphabets are extracted.
Method #1: Using isalpha() + all() + list comprehension
In this, we check for all the alphabets using isalpha() and all() is used to ensure all the strings contain just the alphabets. The list comprehension is used to iterate through rows.
Python3
# Python3 code to demonstrate working of
# Filter rows with only Alphabets
# Using isalpha() + all() + list comprehension
# initializing list
test_list = [["gfg", "is", "best"], ["Geeks4Geeks", "good"],
["Gfg is good"], ["love", "gfg"]]
# printing original lists
print("The original list is : " + str(test_list))
# all() checks for all strings to contain alphabets
res = [sub for sub in test_list if all(ele.isalpha() for ele in sub)]
# printing result
print("Filtered Rows : " + str(res))
OutputThe original list is : [['gfg', 'is', 'best'], ['Geeks4Geeks', 'good'], ['Gfg is good'], ['love', 'gfg']]
Filtered Rows : [['gfg', 'is', 'best'], ['love', 'gfg']]
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + join() + isalpha()
In this, we concatenate each string using join() and test if its all alphabets using isalpha(), and add if the verdict returns true.
Python3
# Python3 code to demonstrate working of
# Filter rows with only Alphabets
# Using filter() + lambda + join() + isalpha()
# initializing list
test_list = [["gfg", "is", "best"], ["Geeks4Geeks", "good"],
["Gfg is good"], ["love", "gfg"]]
# printing original lists
print("The original list is : " + str(test_list))
# join() used to concatenate strings
res = list(filter(lambda sub: ''.join(
[ele for ele in sub]).isalpha(), test_list))
# printing result
print("Filtered Rows : " + str(res))
OutputThe original list is : [['gfg', 'is', 'best'], ['Geeks4Geeks', 'good'], ['Gfg is good'], ['love', 'gfg']]
Filtered Rows : [['gfg', 'is', 'best'], ['love', 'gfg']]
The time and space complexity for all the methods are the same:
Time Complexity: O(n2)
Auxiliary Space : O(n)
Method #3 : Using itertools.filterfalse() method
Python3
# Python3 code to demonstrate working of
# Filter rows with only Alphabets
import itertools
# initializing list
test_list = [["gfg", "is", "best"], ["Geeks4Geeks", "good"],
["Gfg is good"], ["love", "gfg"]]
# printing original lists
print("The original list is : " + str(test_list))
# join() used to concatenate strings
res = list(itertools.filterfalse(lambda sub: not ''.join(
[ele for ele in sub]).isalpha(), test_list))
# printing result
print("Filtered Rows : " + str(res))
OutputThe original list is : [['gfg', 'is', 'best'], ['Geeks4Geeks', 'good'], ['Gfg is good'], ['love', 'gfg']]
Filtered Rows : [['gfg', 'is', 'best'], ['love', 'gfg']]
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method #4 : Using re
Here's another approach that you can use to extract the rows that contain only alphabets in its strings.
This approach uses the built-in re module to match the string against a regular expression pattern for alphabets and returns the filtered rows.
Python3
import re
test_list = [["gfg", "is", "best"], ["Geeks4Geeks", "good"], ["Gfg is good"], ["love", "gfg"]]
result = [row for row in test_list if all(re.match(r'^[a-zA-Z]+$', ele) for ele in row)]
print("Filtered Rows:", result)
OutputFiltered Rows: [['gfg', 'is', 'best'], ['love', 'gfg']]
Time Complexity: O(n^2)
Auxiliary Space: O(n)
Note: In the above code, re.match returns a match object if there is a match, and None if there is no match. The all function returns True if all the elements in the given iterable are True, and False otherwise. In this case, we use all to check if all the elements in the row list match the regular expression pattern for alphabets.
Method #5: Using a nested loop to iterate over each element and character of the list and check if it is an alphabet.
Step-by-step approach:
- Initialize an empty list to store the filtered rows.
- Use a nested for loop to iterate over each sublist and each character in the sublist.
- Use the isalpha() method to check if the character is an alphabet.
- If all characters in the sublist are alphabets, append the sublist to the filtered list.
- Return the filtered list.
Python3
# Python3 code to demonstrate working of
# Filter rows with only Alphabets
# initializing list
test_list = [["gfg", "is", "best"], ["Geeks4Geeks", "good"],
["Gfg is good"], ["love", "gfg"]]
# printing original lists
print("The original list is : " + str(test_list))
# Using nested loop to filter rows
filtered_list = []
for sublist in test_list:
is_alphabetic = True
for char in sublist:
if not char.isalpha():
is_alphabetic = False
break
if is_alphabetic:
filtered_list.append(sublist)
# printing result
print("Filtered Rows : " + str(filtered_list))
OutputThe original list is : [['gfg', 'is', 'best'], ['Geeks4Geeks', 'good'], ['Gfg is good'], ['love', 'gfg']]
Filtered Rows : [['gfg', 'is', 'best'], ['love', 'gfg']]
Time complexity: O(n*m), where n is the number of sublists and m is the length of the longest sublist.
Auxiliary space: O(k), where k is the number of alphabetic sublists.
Similar Reads
Filter a list based on the Given List of Strings - Python The task of filtering a list based on given strings involves removing elements that contain specific keywords. Given a list of file paths and filter words, the goal is to exclude paths containing any specified words. For example, with filter words ['key', 'keys', 'keyword'] and file paths ['home/key
3 min read
Python - Filter rows with Elements as Multiple of K Given a Matrix, extract rows with elements multiple of K. Input : test_list = [[5, 10, 15], [4, 8, 12], [100, 15], [5, 10, 23]], K = 4 Output : [[4, 8, 12]] Explanation : All are multiples of 4. Input : test_list = [[5, 10, 15], [4, 8, 11], [100, 15], [5, 10, 23]], K = 4 Output : [] Explanation : No
6 min read
Python - Filter list of strings based on the substring list The problem requires to check which strings in the main list contain any of the substrings from a given list and keep only those that match. Let us explore this problem and understand different methods to solve it.Using list comprehension with any() (Most Efficient)List comprehension is a concise an
4 min read
Python | Extract Strings with only Alphabets In Python, extracting strings that contain only alphabetic characters involves filtering out any strings that include numbers or special characters. The most efficient way to achieve this is by using the isalpha() method, which checks if all characters in a string are alphabetic.Pythonli= ['gfg', 'i
2 min read
Python - Rows with all List elements Given a Matrix, get all the rows with all the list elements. Input : test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]], sub_list = [1, 2] Output : [[2, 1, 8], [6, 1, 2]] Explanation : Extracted lists have 1 and 2. Input : test_list = [[7, 6, 3, 2], [5, 6], [2, 1, 8], [6, 1, 2]], sub_list = [2
8 min read