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
test_list = [[ 1 , True , 2 ], [ False , False , 3 ],
[ False , False , False ], [ 1 , 0 , 1 ]]
print ( "The original list : " + str (test_list))
res = [sub for sub in test_list
if sub.count( False ) ! = len (sub)]
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
test_list = [[ 1 , True , 2 ], [ False , False , 3 ],
[ False , False , False ], [ 1 , 0 , 1 ]]
print ( "The original list : " + str (test_list))
res = [sub for sub in test_list if set (sub) ! = { False }]
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
test_list = [[ 1 , True , 2 ], [ False , False , 3 ],
[ False , False , False ], [ 1 , 0 , 1 ]]
print ( "The original list : " + str (test_list))
res = []
for i in test_list:
if ([i[ 0 ]] * len (i) ! = i):
res.append(i)
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
test_list = [[ 1 , True , 2 ], [ False , False , 3 ],
[ False , False , False ], [ 1 , 0 , 1 ]]
print ( "The original list : " + str (test_list))
res = []
for i in test_list:
tem = list ( filter ( lambda x: x ! = False , i))
if len (tem) ! = 0 :
res.append(i)
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
test_list = [[ 1 , True , 2 ], [ False , False , 3 ],
[ False , False , False ], [ 1 , 0 , 1 ]]
print ( "Original matrix:" , test_list)
res = [row for row in test_list if any (row)]
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
import operator as op
test_list = [[ 1 , True , 2 ], [ False , False , 3 ],
[ False , False , False ], [ 1 , 0 , 1 ]]
print ( "The original list : " + str (test_list))
res = [sub for sub in test_list
if op.countOf(sub, False ) ! = len (sub)]
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
test_list = [[ 1 , True , 2 ], [ False , False , 3 ],
[ False , False , False ], [ 1 , 0 , 1 ]]
print ( "The original list : " + str (test_list))
res = []
for row in test_list:
if not all (val is False for val in row):
res.append(row)
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:
return matrix
elif all (val is False for val in matrix[ 0 ]):
return remove_false_rows(matrix[ 1 :])
else :
return [matrix[ 0 ]] + remove_false_rows(matrix[ 1 :])
test_list = [[ 1 , True , 2 ], [ False , False , 3 ],
[ False , False , False ], [ 1 , 0 , 1 ]]
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)
Similar Reads
Python | Remove last element from each row in Matrix
Sometimes, while working with Matrix data, we can have a stray element attached at rear end of each row of matrix. This can be undesired at times and wished to be removed. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + del + list slicing The combination of th
6 min read
Python - Remove front column from Matrix
Sometimes, while working with Matrix data, we can have stray element that attached at front end of each row of matrix. This can be undesired at times and wished to be removed. Letâs discuss certain ways in which this task can be performed. Method #1: Using loop + del + list slicing The combination o
6 min read
Python - Remove Similar Rows from Tuple Matrix
Sometimes, while working with Tuple Matrix, we can have a problem in which we get lots of data, which are similar, i.e elements are same in rows, just the ordering of tuples is different, it's sometimes, desired to get them removed. This kind of problem can have application in domains such as web de
9 min read
Python | Remove duplicates in Matrix
While working with Python Matrix, we can face a problem in which we need to perform the removal of duplicates from Matrix. This problem can occur in Machine Learning domain because of extensive usage of matrices. Let's discuss certain way in which this task can be performed. Method : Using loop This
2 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 | 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 Item from Dictionary
There are situations where we might want to remove a specific key-value pair from a dictionary. For example, consider the dictionary d = {'x': 10, 'y': 20, 'z': 30}. If we need to remove the key 'y', there are multiple ways to achieve this. Let's discuss several methods to remove an item from a dict
3 min read
Python program to Remove the last item from array
Given an array, the task is to write a Python program to remove the last element in Python. Example: Input: ["geeks", "for", "geeks"] Output: ["geeks", "for"] Input: [1, 2, 3, 4, 5] Output: [1, 2, 3, 4] Explanation: Here simply we have to remove the last element present in the array and return the r
4 min read
Remove Multiple Elements from List in Python
In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list. [GFGTABS] Python a = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remov
3 min read
Python Program to Remove First Diagonal Elements from a Square Matrix
Given a square matrix of N*N dimension, the task is to write a Python program to remove the first diagonal. Examples: Input : test_list = [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]] Output : [[3, 3, 2, 1], [5, 7, 8, 2], [9, 3, 6, 7], [0, 1, 2, 5], [2, 5, 4,
6 min read