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
test_list = [[ "gfg" , "is" , "best" ], [ "Geeks4Geeks" , "good" ],
[ "Gfg is good" ], [ "love" , "gfg" ]]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if all (ele.isalpha() for ele in sub)]
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
test_list = [[ "gfg" , "is" , "best" ], [ "Geeks4Geeks" , "good" ],
[ "Gfg is good" ], [ "love" , "gfg" ]]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda sub: ''.join(
[ele for ele in sub]).isalpha(), test_list))
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
import itertools
test_list = [[ "gfg" , "is" , "best" ], [ "Geeks4Geeks" , "good" ],
[ "Gfg is good" ], [ "love" , "gfg" ]]
print ( "The original list is : " + str (test_list))
res = list (itertools.filterfalse( lambda sub: not ''.join(
[ele for ele in sub]).isalpha(), test_list))
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
test_list = [[ "gfg" , "is" , "best" ], [ "Geeks4Geeks" , "good" ],
[ "Gfg is good" ], [ "love" , "gfg" ]]
print ( "The original list is : " + str (test_list))
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)
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.
Similar Reads
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 a
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. [GFGTABS] Python li
3 min read
Sorting List of Lists with First Element of Each Sub-List in Python
In Python, sorting a list of lists by the first element of each sub-list is a common task. Whether you're dealing with data points, coordinates, or any other structured information, arranging the lists based on the values of their first elements can be crucial. In this article, we will sort a list o
3 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
Python | Convert List of lists to list of Strings
Interconversion of data is very popular nowadays and has many applications. In this scenario, we can have a problem in which we need to convert a list of lists, i.e matrix into list of strings. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + joi
4 min read
Create List of Substrings from List of Strings in Python
In Python, when we work with lists of words or phrases, we often need to break them into smaller pieces, called substrings. A substring is a contiguous sequence of characters within a string. Creating a new list of substrings from a list of strings can be a common task in various applications. In th
3 min read
Python - Convert list of strings and characters to list of characters
Sometimes we come forward to the problem in which we receive a list that consists of strings and characters mixed and the task we need to perform is converting that mixed list to a list consisting entirely of characters. Using itertools.chain()itertools.chain() combines multiple lists or iterables i
3 min read
Iterate Over a List of Lists in Python
We are given a list that contains multiple sublists, and our task is to iterate over each of these sublists and access their elements. For example, if we have a list like this: [[1, 2], [3, 4], [5, 6]], then we need to loop through each sublist and access elements like 1, 2, 3, and so on. Using Nest
2 min read
Python - Filter Tuples by Kth element from List
Given a list of tuples, filter by Kth element presence in List. Input : test_list = [("GFg", 5, 9), ("is", 4, 3), ("best", 10, 29)], check_list = [4, 2, 3, 10], K = 2 Output : [('is', 4, 3)] Explanation : 3 is 2nd element and present in list, hence filtered tuple. Input : test_list = [("GFg", 5, 9),
5 min read
Python - Filter all uppercase characters Tuples from given list of tuples
Given a Tuple list, filter tuples that contain all uppercase characters. Input : test_list = [("GFG", "IS", "BEST"), ("GFg", "AVERAGE"), ("GfG", ), ("Gfg", "CS")] Output : [('GFG', 'IS', 'BEST')] Explanation : Only 1 tuple has all uppercase Strings. Input : test_list = [("GFG", "iS", "BEST"), ("GFg"
8 min read