Python program to remove row with custom list element
Last Updated :
25 Mar, 2023
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]]
Explanation : [5, 3, 1] row has 3 in it in custom list, hence omitted.
Input : test_list = [[5, 3, 1], [7, 8, 19], [1, 10, 22], [12, 18, 20]], check_list = [3, 10, 19, 29, 20, 15]
Output : []
Explanation : All rows have some element from custom list, hence omitted.
Method 1: Using any() and list comprehension
In this, we perform the task of checking for any elements from custom list to check for rows using any() and list comprehension is used to omit row if any element from custom list is found in row.
Example:
Python3
# initializing Matrix
test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
# printing original list
print("The original list is : " + str(test_list))
# initializing custom list
check_list = [3, 10, 19, 29, 20, 15]
# list comprehension used to omit rows from matrix
# any() checks for any element found from check list
res = [row for row in test_list if not any(el in row for el in check_list)]
# printing result
print("The omitted rows matrix : " + str(res))
OutputThe original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Method 2 : Using filter(), lambda and any()
Similar to above method, only difference being filter() and lambda function is used to perform task of filtering out or omit rows from matrix from result.
Example:
Python3
# initializing Matrix
test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
# printing original list
print("The original list is : " + str(test_list))
# initializing custom list
check_list = [3, 10, 19, 29, 20, 15]
# filter() used to perform filtering
# any() checks for any element found from check list
res = list(filter(lambda row: not any(
el in row for el in check_list), test_list))
# printing result
print("The omitted rows matrix : " + str(res))
OutputThe original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Method 3 : Using Counter() function
Python3
from collections import Counter
# initializing Matrix
test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
# printing original list
print("The original list is : " + str(test_list))
# initializing custom list
check_list = [3, 10, 19, 29, 20, 15]
freq = Counter(check_list)
for i in test_list.copy():
for j in i:
if j in freq.keys():
test_list.remove(i)
break
# printing result
print("The omitted rows matrix : " + str(test_list))
OutputThe original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Method #4:Using operator.countOf() method
Python3
from collections import Counter
import operator as op
# initializing Matrix
test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
# printing original list
print("The original list is : " + str(test_list))
# initializing custom list
check_list = [3, 10, 19, 29, 20, 15]
freq = Counter(check_list)
for i in test_list.copy():
for j in i:
if op.countOf(freq.keys(), j) > 0:
test_list.remove(i)
break
# printing result
print("The omitted rows matrix : " + str(test_list))
OutputThe original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Method #5: Using for loop:
Python3
# initializing Matrix
test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
# printing original list
print("The original list is : " + str(test_list))
# initializing custom list
check_list = [3, 10, 19, 29, 20, 15]
# using a for loop to iterate through the matrix
res = []
for row in test_list:
if not any(el in row for el in check_list):
res.append(row)
# printing result
print("The omitted rows matrix : " + str(res))
#This code is contributed by pinjala Jyothi.
OutputThe original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Method#6: Using Recursive method.
Here, the function omit_rows_recursive takes a matrix matrix and a list check_list as input. The base case is when the matrix is empty, in which case an empty list is returned. If the first row of the matrix contains any elements from the check list, the function recursively calls itself with the remaining part of the matrix after the first row. Otherwise, the function concatenates the first row with the result of recursively calling itself with the remaining part of the matrix after the first row.
Python3
def omit_rows_recursive(matrix, check_list):
if not matrix:
return []
if any(el in matrix[0] for el in check_list):
return omit_rows_recursive(matrix[1:], check_list)
else:
return [matrix[0]] + omit_rows_recursive(matrix[1:], check_list)
# initializing Matrix
test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
# printing original list
print("The original list is : " + str(test_list))
# initializing custom list
check_list = [3, 10, 19, 29, 20, 15]
# using a for loop to iterate through the matrix
res = omit_rows_recursive(test_list,check_list)
# printing result
print("The omitted rows matrix : " + str(res))
#This code is contributed by tvsk.
OutputThe original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
The time complexity of the recursive function is also O(nm), as it must examine each element of each row in the matrix once.
The auxiliary space of the recursive function is also O(nm), as it may need to store up to n rows of m elements each in the result. However, the recursive function may have additional space complexity due to the function call stack, depending on the implementation.
Similar Reads
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 program to extract rows with common difference elements
Given a Matrix, extract rows with AP sequence. Input : test_list = [[4, 7, 10], [8, 10, 12], [10, 11, 13], [6, 8, 10]] Output : [[4, 7, 10], [8, 10, 12], [6, 8, 10]] Explanation : 3, 4, and 2 are common difference in AP. Input : test_list = [[4, 7, 10], [8, 10, 13], [10, 11, 13], [6, 8, 10]] Output
3 min read
Python Program to Remove Palindromic Elements from a List
Given a list, the task here is to write Python programs that can remove all the elements which have palindromic equivalent element present in the list. Examples: Input : test_list = [54, 67, 12, 45, 98, 76, 9] Output : [12, 98] Explanation : 67 has 76 as palindromic element, both are omitted. Input
2 min read
Python program to remove duplicate elements index from other list
Given two lists, the task is to write a Python program to remove all the index elements from 2nd list which are duplicate element indices from 1st list. Examples: Input : test_list1 = [3, 5, 6, 5, 3, 7, 8, 6], test_list2 = [1, 7, 6, 3, 7, 9, 10, 11] Output : [1, 7, 6, 9, 10] Explanation : 3, 7 and 1
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 - 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 program to omit K length Rows
Given a Matrix, remove rows with length K. Input : test_list = [[4, 7], [8, 10, 12, 8], [10, 11], [6, 8, 10]], K = 2 Output : [[8, 10, 12, 8], [6, 8, 10]] Explanation : [4, 7] and [10, 11] omitted as length 2 rows. Input : test_list = [[4, 7], [8, 10, 12, 8], [10, 11], [6, 8, 10]], K = 3 Output : [[
3 min read
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 Program to sort rows of a matrix by custom element count
Given Matrix, the following program shows how to sort rows of a matrix by the count of presence of numbers from a specified list. Input : test_list = [[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]], cus_list = [4, 5, 7] Output : [[9, 8, 2], [6, 5], [7, 1], [4, 5, 1, 7]] Explanation : 0 < 1 = 1 < 3 i
5 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