Python – Remove rows with Numbers
Last Updated :
22 Apr, 2023
Given a Matrix, remove rows with integer instances.
Input : test_list = [[4, ‘Gfg’, ‘best’], [‘gfg’, 5, ‘is’, ‘best’], [3, 5], [‘GFG’, ‘Best’]]
Output : [[‘GFG’, ‘Best’]]
Explanation : All rows with numbers are removed.
Input : test_list = [[4, ‘Gfg’, ‘best’], [‘gfg’, 5, ‘is’, ‘best’], [3, 5], [‘GFG’, ‘Best’, 1]]
Output : []
Explanation : All rows with numbers are removed. No list left.
Method #1 : Using any() + list comprehension
In this, we check for any integer element using any() in any row, and use list comprehension to perform task of iteration.
Python3
test_list = [[ 4 , 'Gfg' , 'best' ], [
'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if not any (
isinstance (ele, int ) for ele in sub)]
print ( "The filtered rows : " + str (res))
|
Output:
The original list is : [[4, ‘Gfg’, ‘best’], [‘gfg’, ‘is’, ‘best’], [3, 5], [‘GFG’, ‘Best’]] The filtered rows : [[‘gfg’, ‘is’, ‘best’], [‘GFG’, ‘Best’]]
Method #2 : Using filter() + lambda + any()
In this, we perform task of filtering using lambda and filter(), any() is used in similar way as in above method.
Python3
test_list = [[ 4 , 'Gfg' , 'best' ], [
'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda sub: not any ( isinstance (ele, int )
for ele in sub), test_list))
print ( "The filtered rows : " + str (res))
|
Output:
The original list is : [[4, ‘Gfg’, ‘best’], [‘gfg’, ‘is’, ‘best’], [3, 5], [‘GFG’, ‘Best’]] The filtered rows : [[‘gfg’, ‘is’, ‘best’], [‘GFG’, ‘Best’]]
Method #3 : Using list(),map(),join() and isalpha() methods
Python3
test_list = [[ 4 , 'Gfg' , 'best' ], [
'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
a = list ( map ( str ,i))
a = "".join(a)
if (a.isalpha()):
res.append(i)
print ( "The filtered rows : " + str (res))
|
Output
The original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
Method #4 : Using list(),map(),join(),replace() and len() methods
Python3
test_list = [[ 4 , 'Gfg' , 'best' ], [
'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
print ( "The original list is : " + str (test_list))
res = []
numbers = "0123456789"
for i in test_list:
a = list ( map ( str ,i))
a = "".join(a)
x = a
for j in numbers:
a = a.replace(j,"")
if ( len (x) = = len (a)):
res.append(i)
print ( "The filtered rows : " + str (res))
|
Output
The original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
The time complexity of this code is O(n*m), where n is the length of the input list, and m is the length of the longest row in the list.
The auxiliary space complexity of this code is O(n), where n is the length of the input list.
Method#5:Using a loop and isnumeric()
Algorithm:
- Initialize an empty list “res”
- Loop through each row of the input list “test_list”
- Convert each element of the row to a string using the “map” function and the “str” method
- Concatenate the string elements of the row using the “join” method to create a single string “a”
- Check if the string “a” contains only alphabetic characters using the “isalpha” method
- If the string contains only alphabetic characters, append the row to the “res” list
- Return the filtered rows list “res”
- Print the filtered rows list
Python3
test_list = [[ 4 , 'Gfg' , 'best' ], [ 'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
print ( "The original list is : " + str (test_list))
res = []
for sublist in test_list:
flag = True
for ele in sublist:
if isinstance (ele, int ) or ele.isnumeric():
flag = False
break
if flag:
res.append(sublist)
print ( "The filtered rows : " + str (res))
|
Output
The original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
Time complexity: O(n*m) where n is the number of rows and m is the number of columns in the input list “test_list”
Auxiliary Space: O(1) (ignoring the space required to store the filtered rows list)
Method 6: using the set() function and set operations.
Here are the steps:
- Define the input list test_list.
- Convert each sublist in test_list into a set using the set() function. This will remove duplicates and convert all elements into a set of unique values.
- Check if the set contains any integer or numeric values using the any() function and the isdigit() string method. The any() function returns True if at least one element in an iterable is True, and False otherwise.
- If the set does not contain any integers or numeric values, append the original sublist to a new list res.
- Print the filtered sublists.
Python3
test_list = [[ 4 , 'Gfg' , 'best' ], [ 'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
res = []
for sublist in test_list:
set_sublist = set (sublist)
if not any ( str (element).isdigit() or isinstance (element, int ) for element in set_sublist):
res.append(sublist)
print ( "The filtered rows: " , res)
|
Output
The filtered rows: [['gfg', 'is', 'best'], ['GFG', 'Best']]
Time complexity: O(n*m), where n is the length of test_list and m is the length of the longest sublist in test_list.
Auxiliary space: O(m), because it uses a set to store each sublist and the size of each set is at most the length of the longest sublist in test_list.
Method 7: using reduce():
- Import the reduce() function from the functools module.
- Define the list of lists test_list.
- Define a lambda function that takes an accumulator acc and a current list x as input. If all elements in x are non-numeric strings, the lambda function appends x to acc. Otherwise, it returns acc as is.
- Use reduce() to apply the lambda function to each sublist in test_list. The initial value of the accumulator is an empty list.
- Store the resulting filtered list in the variable res.
- Print the original list and the filtered list.
Python3
from functools import reduce
test_list = [[ 4 , 'Gfg' , 'best' ], [
'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
res = reduce ( lambda acc, x: acc + [x] if all ( isinstance (ele, str )
and not ele.isnumeric() for ele in x) else acc, test_list, [])
print ( "The original list is : " + str (test_list))
print ( "The filtered rows : " + str (res))
|
Output
The original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
Time complexity:
The lambda function is applied to each sublist in test_list exactly once. The time complexity of the lambda function is O(m), where m is the length of the sublist. Therefore, the time complexity of the reduce() function is O(nm), where n is the length of test_list.
Space complexity:
The space complexity of this code is O(n), where n is the length of test_list. This is because we create a new list for each sublist that satisfies the condition in the lambda function, and the maximum number of sublists that can satisfy this condition is n
Similar Reads
Python - Remove numbers with repeating digits
Given a list of numbers, the task is to write a Python program to remove all numbers with repetitive digits. Examples: Input : test_list = [4252, 6578, 3421, 6545, 6676]Output : test_list = [6578, 3421]Explanation : 4252 has 2 occurrences of 2 hence removed. Similar case for all other removed. Input
5 min read
Python - Remove positional rows
Given a Matrix, the task is to write a Python program to remove rows that have certain positions. Example: Input: test_list = [[3, 5, 2], [1, 8, 9], [5, 3, 1], [0, 1, 6], [9, 4, 1], [1, 2, 10], [0, 1, 2]]; idx_lis = [1, 2, 5] Output: [[3, 5, 2], [0, 1, 6], [9, 4, 1], [0, 1, 2]] Explanation: 1st, 2nd
7 min read
Python - Retain Numbers in String
Retaining numbers in a string involves extracting only the numeric characters while ignoring non-numeric ones. Using List Comprehensionlist comprehension can efficiently iterate through each character in the string, check if it is a digit using the isdigit() method and join the digits together to fo
2 min read
Python program to remove row with custom list element
Given a matrix, the task here is to write a Python program to remove rows that have any element from the custom list and then display the result. Examples: Input : test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]], check_list = [3, 10, 19, 29, 20, 15] Output : [[7, 8, 9], [12, 18, 21]] E
6 min read
Python | Remove similar element rows in tuple Matrix
Sometimes, while working with data, we can have a problem in which we need to remove elements from the tuple matrix on a condition that if all elements in row of tuple matrix is same. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + all() This ta
6 min read
Python - Remove Rows for similar Kth column element
Given a Matrix, remove row if similar element has occurred in row above in Kth column. Input : test_list = [[3, 4, 5], [2, 3, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]], K = 2 Output : [[3, 4, 5], [10, 4, 3], [7, 8, 9], [9, 3, 6]] Explanation : In [2, 3, 5], we already has list [3, 4, 5] having 5 at K, i
7 min read
Python - Remove the row if all elements equal to N
Sometimes, while handling data, especially in the Machine Learning domain, we need to go through a lot of similar N-equal data. We sometimes need to eliminate the rows which are all equal to N. Letâs discuss certain ways to remove the rows that have all N values as list columns. Method #1: Using lis
5 min read
Python | Remove False row from matrix
Sometimes, while handling data, especially in the Machine Learning domain, we need to go through a lot of incomplete or empty data. We sometimes need to eliminate the rows which do not contain a value in any of the columns. Let's discuss certain ways to remove the rows that have all False values as
9 min read
Python program to remove rows with duplicate element in Matrix
Given Matrix, remove all rows which have duplicate elements in them. Input : test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [[4, 3, 2]] Explanation : [4, 3, 2] is the only unique row. Input : test_list = [[4, 3, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [] Explanation : No
5 min read
Python | Remove given element from the list
Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 min read