Python - Remove the row if all elements equal to N
Last Updated :
17 Apr, 2023
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 list comprehension + count() + len() We can perform this particular task using the list comprehension recipe, partnered with the combination of len and count function to check for similarity element counter equating to the length of list.
Python3
# Python3 code to demonstrate
# N row deletion in Matrix
# using list comprehension + count() + len()
# initializing matrix
test_list = [[1, 4, 2], [False, 9, 3],
[6, 6, 6], [1, 0, 1]]
# printing original list
print("The original list : " + str(test_list))
# initializing N
N = 6
# using list comprehension + count() + len()
# N row deletion in Matrix
res = [sub for sub in test_list if sub.count(N) != len(sub)]
# print result
print("The list after removal of N rows : " + str(res))
Output : The original list : [[1, 4, 2], [False, 9, 3], [6, 6, 6], [1, 0, 1]]
The list after removal of N rows : [[1, 4, 2], [False, 9, 3], [1, 0, 1]]
Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.
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 N set for equality and removing if a match is found.
Python3
# Python3 code to demonstrate
# N row deletion in Matrix
# using list comprehension + set()
# initializing matrix
test_list = [[1, 4, 2], [False, 9, 3],
[6, 6, 6], [1, 0, 1]]
# printing original list
print("The original list : " + str(test_list))
# initializing N
N = 6
# using list comprehension + set()
# N row deletion in Matrix
res = [sub for sub in test_list if set(sub) != {N}]
# print result
print("The list after removal of N rows : " + str(res))
Output : The original list : [[1, 4, 2], [False, 9, 3], [6, 6, 6], [1, 0, 1]]
The list after removal of N rows : [[1, 4, 2], [False, 9, 3], [1, 0, 1]]
Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.
Method #3 : Using len() method
Python3
# Python3 code to demonstrate
# N row deletion in Matrix
# initializing matrix
test_list = [[1, 4, 2], [False, 9, 3],
[6, 6, 6], [1, 0, 1]]
# printing original list
print("The original list : " + str(test_list))
# initializing N
N = 6
res = []
for i in test_list:
if([i[0]]*len(i) != i):
res.append(i)
# print result
print("The list after removal of N rows : " + str(res))
OutputThe original list : [[1, 4, 2], [False, 9, 3], [6, 6, 6], [1, 0, 1]]
The list after removal of N rows : [[1, 4, 2], [False, 9, 3], [1, 0, 1]]
Time complexity: O(M^N) as the number of combinations generated is M choose N.
Auxiliary space: O(M^N) as the size of the resultant list is also M choose N.
Method #4 : Using remove()+filter()+copy() +lambda functions
Python3
# Python3 code to demonstrate
# N row deletion in Matrix
# initializing matrix
test_list = [[1, 4, 2], [False, 9, 3],
[6, 6, 6], [1, 0, 1]]
# printing original list
print("The original list : " + str(test_list))
# initializing N
N = 6
for i in test_list.copy():
temp = list(filter(lambda x: x == 6, i))
if(len(temp) == len(i)):
test_list.remove(i)
# print result
print("The list after removal of N rows : " + str(test_list))
OutputThe original list : [[1, 4, 2], [False, 9, 3], [6, 6, 6], [1, 0, 1]]
The list after removal of N rows : [[1, 4, 2], [False, 9, 3], [1, 0, 1]]
Time Complexity: O(N*N)
Auxiliary Space : O(1)
Method #5 : Using a for loop
Python3
test_list = [[1, 4, 2], [False, 9, 3],
[6, 6, 6], [1, 0, 1]]
# printing original list
print("The original list : " + str(test_list))
N = 6
result = []
for row in test_list:
if N not in row:
result.append(row)
# print result
print("The list after removal of N rows : " + str(result))
#This code is contributed by Jyothi pinjala
OutputThe original list : [[1, 4, 2], [False, 9, 3], [6, 6, 6], [1, 0, 1]]
The list after removal of N rows : [[1, 4, 2], [False, 9, 3], [1, 0, 1]]
Time Complexity: O(N)
Auxiliary Space : O(N)
Method #6 : Using operator.countOf() method
Approach
- Initiated for loop to traverse the nested list
- Used if condition to check whether count(operator.countOf()) of N is equal to length of list
- If the condition is satisfied append the row to output list
- Display the output list
Python3
# Python3 code to demonstrate
# N row deletion in Matrix
# initializing matrix
test_list = [[1, 4, 2], [False, 9, 3],
[6, 6, 6], [1, 0, 1]]
# printing original list
print("The original list : " + str(test_list))
# initializing N
N = 6
res = []
import operator
for i in test_list:
if(operator.countOf(i,N)!=len(i)):
res.append(i)
# print result
print("The list after removal of N rows : " + str(res))
OutputThe original list : [[1, 4, 2], [False, 9, 3], [6, 6, 6], [1, 0, 1]]
The list after removal of N rows : [[1, 4, 2], [False, 9, 3], [1, 0, 1]]
Time Complexity: O(N)
Auxiliary Space : O(N)
Similar Reads
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 - Retain all K elements Rows Sometimes, while working with Python lists, we can have a problem in which we need to retain rows which have only K as elements. This kind of application can occur in data domains which take Matrix as input. Let's discuss certain ways in which this task can be performed. Input : test_list = [[7, 6],
8 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 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
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 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