Open In App

Python – Remove rows with Numbers

Last Updated : 22 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate working of
# Remove rows with Numbers
# Using list comprehension + any
 
# initializing lists
test_list = [[4, 'Gfg', 'best'], [
    'gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# using isinstance to check for integer and not include them
res = [sub for sub in test_list if not any(
    isinstance(ele, int) for ele in sub)]
 
# printing result
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




# Python3 code to demonstrate working of
# Remove rows with Numbers
# Using filter() + lambda + any()
 
# initializing lists
test_list = [[4, 'Gfg', 'best'], [
    'gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
 
# printing original lists
print("The original list is : " + str(test_list))
 
# using isinstance to check for integer and not include them
# filter() used to filter with lambda fnc.
res = list(filter(lambda sub: not any(isinstance(ele, int)
                                      for ele in sub), test_list))
 
# printing result
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




# Python3 code to demonstrate working of
# Remove rows with Numbers
 
# initializing lists
test_list = [[4, 'Gfg', 'best'], [
    'gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
 
# printing original lists
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)
# printing result
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




# Python3 code to demonstrate working of
# Remove rows with Numbers
 
# initializing lists
test_list = [[4, 'Gfg', 'best'], [
    'gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
 
# printing original lists
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)
     
# printing result
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:

  1. Initialize an empty list “res”
  2. Loop through each row of the input list “test_list”
  3. Convert each element of the row to a string using the “map” function and the “str” method
  4. Concatenate the string elements of the row using the “join” method to create a single string “a”
  5. Check if the string “a” contains only alphabetic characters using the “isalpha” method
  6. If the string contains only alphabetic characters, append the row to the “res” list
  7. Return the filtered rows list “res”
  8. Print the filtered rows list

Python3




test_list = [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
 
# printing original lists
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))
#This code is contributed by Vinay Pinjala.


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:

  1. Define the input list test_list.
  2. 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.
  3. 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.
  4. If the set does not contain any integers or numeric values, append the original sublist to a new list res.
  5. Print the filtered sublists.

Python3




# Define input list
test_list = [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
 
# Initialize empty list for filtered sublists
res = []
 
# Iterate over each sublist in test_list
for sublist in test_list:
    # Convert sublist to set
    set_sublist = set(sublist)
    # Check if set contains any integers or numeric values
    if not any(str(element).isdigit() or isinstance(element, int) for element in set_sublist):
        # Append original sublist to res if set does not contain integers or numeric values
        res.append(sublist)
 
# Print filtered sublists
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():

  1. Import the reduce() function from the functools module.
  2. Define the list of lists test_list.
  3. 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.
  4. Use reduce() to apply the lambda function to each sublist in test_list. The initial value of the accumulator is an empty list.
  5. Store the resulting filtered list in the variable res.
  6. Print the original list and the filtered list.

Python3




from functools import reduce
 
# Driver Code
test_list = [[4, 'Gfg', 'best'], [
    'gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
 
# User reduce() to filter the list of lists
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 original list and filtered rows
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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads