Open In App

Python | Remove False row from matrix

Improve
Improve
Like Article
Like
Save
Share
Report

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 list columns. 

Method #1 : Using list comprehension + count() + len() We can perform this particular task using the list comprehension recipe, partnered with the combination of len and count functions to check for similarity element counter equating to the length of the list. 

Python3




# Python3 code to demonstrate
# removing False rows in matrix
# using list comprehension + count() + len()
 
# initializing matrix
test_list = [[1, True, 2], [False, False, 3],
             [False, False, False], [1, 0, 1]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + count() + len()
# removing False rows in matrix
res = [sub for sub in test_list
       if sub.count(False) != len(sub)]
 
# print result
print("The list after removal of False rows : " + str(res))


Output

The original list : [[1, True, 2], [False, False, 3], [False, False, False], [1, 0, 1]]
The list after removal of False rows : [[1, True, 2], [False, False, 3], [1, 0, 1]]

Time complexity: O(n) where n is the number of rows in the matrix.
Auxiliary Space: O(m) where m is the length of the final resulting list after removing False rows.

Method #2 : Using list comprehension + set() This particular task can also be performed by converting the entire row into a set and then checking for the single value False set for equality and removing if a match is found. 

Python3




# Python3 code to demonstrate
# removing False rows in matrix
# using list comprehension + set()
 
# initializing matrix
test_list = [[1, True, 2], [False, False, 3],
             [False, False, False], [1, 0, 1]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + set()
# removing False rows in matrix
res = [sub for sub in test_list if set(sub) != {False}]
 
# print result
print("The list after removal of False rows : " + str(res))


Output

The original list : [[1, True, 2], [False, False, 3], [False, False, False], [1, 0, 1]]
The list after removal of False rows : [[1, True, 2], [False, False, 3], [1, 0, 1]]

Time Complexity: O(n * m) where n is the number of rows and m is the number of elements in each row.
Auxiliary Space: O(k) where k is the number of elements in the resultant list after removing False rows.

Method #3 : Using * and len() method

Python3




# Python3 code to demonstrate
# removing False rows in matrix
 
# initializing matrix
test_list = [[1, True, 2], [False, False, 3],
             [False, False, False], [1, 0, 1]]
 
# printing original list
print("The original list : " + str(test_list))
res = []
for i in test_list:
    if([i[0]]*len(i) != i):
        res.append(i)
 
 
# print result
print("The list after removal of False rows : " + str(res))


Output

The original list : [[1, True, 2], [False, False, 3], [False, False, False], [1, 0, 1]]
The list after removal of False rows : [[1, True, 2], [False, False, 3], [1, 0, 1]]

Time complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary space: O(km), where k is the number of True rows in the matrix and m is the number of columns. 

Method #4 : Using filter(),append(),lambda functions

Python3




# Python3 code to demonstrate
# removing False rows in matrix
 
# initializing matrix
test_list = [[1, True, 2], [False, False, 3],
             [False, False, False], [1, 0, 1]]
 
# printing original list
print("The original list : " + str(test_list))
 
res = []
# removing False rows in matrix
for i in test_list:
    tem = list(filter(lambda x: x != False, i))
    if len(tem) != 0:
        res.append(i)
 
 
# print result
print("The list after removal of False rows : " + str(res))


Output

The original list : [[1, True, 2], [False, False, 3], [False, False, False], [1, 0, 1]]
The list after removal of False rows : [[1, True, 2], [False, False, 3], [1, 0, 1]]

Time Complexity: O(N*M)
Auxiliary Space: O(N*M)

Method #5 : Using any()

In this code, we first initialize a matrix test_list with a list of lists, containing a mix of True and False values.

We then print the original matrix using the print function.

Next, we use a list comprehension to iterate through each row in the matrix, and use the any function to check if any of the values in the row are True. If any of the values are True, the row is included in the result list. If all of the values are False, the row is not included.

Finally, we print the altered matrix using the print function.

The any function returns True if any of the values in an iterable are True, and False otherwise. In this case, we are using it to check if any of the values in a row of the matrix are True. If any of the values are True, the any function returns True, and the row is included in the result list. If all of the values are False, the any function returns False, and the row is not included in the result list.

Python3




# Initialize the matrix
test_list = [[1, True, 2], [False, False, 3],
             [False, False, False], [1, 0, 1]]
 
# Print the original matrix
print("Original matrix:", test_list)
 
# Use a list comprehension with the all function to remove rows with all False values
res = [row for row in test_list if any(row)]
 
# Print the altered matrix
print("Altered matrix:", res)


Output

Original matrix: [[1, True, 2], [False, False, 3], [False, False, False], [1, 0, 1]]
Altered matrix: [[1, True, 2], [False, False, 3], [1, 0, 1]]

Time Complexity: O(N*M)
Auxiliary Space: O(N*M)

Method #6 : Using operator.countOf() method

Python3




# Python3 code to demonstrate
# removing False rows in matrix
# using list comprehension + operator.countOf() + len()
import operator as op
# initializing matrix
test_list = [[1, True, 2], [False, False, 3],
             [False, False, False], [1, 0, 1]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + operator.countOf() + len()
# removing False rows in matrix
res = [sub for sub in test_list
       if op.countOf(sub,False) != len(sub)]
 
# print result
print("The list after removal of False rows : " + str(res))


Output

The original list : [[1, True, 2], [False, False, 3], [False, False, False], [1, 0, 1]]
The list after removal of False rows : [[1, True, 2], [False, False, 3], [1, 0, 1]]

Time Complexity: O(N*M)
Auxiliary Space: O(N*M)

Method #7: Using a for loop and a new list

Using a for loop to iterate through the rows of the matrix and append the rows that don’t contain only False values to a new list

Step-by-step approach:

  • Initialize the matrix (list of lists) with some values.
  • Print the original list to display the values in the matrix.
  • Create an empty list res to store the non-False rows from the matrix.
  • Iterate through each row in the matrix using a for loop.
  • Use the all() function with a generator expression to check if all the values in the current row are False.
  • If the current row contains at least one non-False value, append it to the res list.
  • Print the res list to display the updated matrix with False rows removed.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate
# removing False rows in matrix
# using a for loop and a new list
 
# initializing matrix
test_list = [[1, True, 2], [False, False, 3],
             [False, False, False], [1, 0, 1]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using a for loop and a new list
# removing False rows in matrix
res = []
for row in test_list:
    if not all(val is False for val in row):
        res.append(row)
 
# print result
print("The list after removal of False rows : " + str(res))


Output

The original list : [[1, True, 2], [False, False, 3], [False, False, False], [1, 0, 1]]
The list after removal of False rows : [[1, True, 2], [False, False, 3], [1, 0, 1]]

Time complexity: O(nm) where n is the number of rows and m is the number of columns in the matrix.
Auxiliary space: O(km) where k is the number of rows that don’t contain only False values (i.e., the size of the result list).

Method#8: Using Recursive method.

Algorithm:
1. If the input matrix is empty, return it.
2. If the first row of the matrix contains only False values, remove it and recursively call the function on the rest of the matrix.
3. Otherwise, keep the first row and recursively call the function on the rest of the matrix.
4. Return the concatenation of the first row (if kept) and the result of the recursive call.

Python3




def remove_false_rows(matrix):
    if not matrix:  # Base case: empty matrix
        return matrix
    elif all(val is False for val in matrix[0]):  # Remove first row if it contains only False values
        return remove_false_rows(matrix[1:])
    else# Keep first row and recursively remove False rows from the rest of the matrix
        return [matrix[0]] + remove_false_rows(matrix[1:])
# initializing matrix
test_list = [[1, True, 2], [False, False, 3],
             [False, False, False], [1, 0, 1]]
# Test the recursive function
print("The original list : " + str(test_list))
res = remove_false_rows(test_list)
print("The list after removal of False rows : " + str(res))


Output

The original list : [[1, True, 2], [False, False, 3], [False, False, False], [1, 0, 1]]
The list after removal of False rows : [[1, True, 2], [False, False, 3], [1, 0, 1]]

Time complexity: O(n*m)
– where n is the number of rows in the matrix and m is the number of columns
– the function needs to examine every value in the matrix once to determine which rows to remove
– the worst-case scenario is when all rows contain only False values

Space complexity: O(n*m)
– the function creates a new matrix (list of lists) to store the result
– the size of the new matrix is proportional to the size of the input matrix, since at worst case all rows may be kept
– in addition, the function uses O(m) space for the call stack during the recursion, where m is the maximum depth of the recursion tree (equal to the number of rows in the original matrix)



Last Updated : 29 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads