Python Filter rows with only Alphabets from List of Lists (original) (raw)
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))
`
Output
The 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))
`
Output
The 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))
`
Output
The 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)
`
Output
Filtered 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))
`
Output
The 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.