Python – Custom Rows Removal depending on Kth Column
Last Updated :
08 May, 2023
Sometimes, while working with Python Matrix, we can have a problem in which we need to remove elements from Matrix depending on its Kth Column element present in the argument list. This can have application in many domains. Let us discuss certain ways in which this task can be performed.
Method #1: Using loop
This is a brute way in which this task can be performed. In this, we iterate rows of the matrix and check for Kth column matching value from list and exclude it from the new list.
Python3
test_list1 = [[ 3 , 4 , 5 ], [ 2 , 6 , 8 ], [ 1 , 10 , 2 ], [ 5 , 7 , 9 ], [ 10 , 1 , 2 ]]
test_list2 = [ 12 , 4 , 6 ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
K = 1
res = []
for ele in test_list1:
if ele[K] not in test_list2:
res.append(ele)
print ("The matrix after rows removal is : " + str (res))
|
Output :
The original list 1 is : [[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]] The original list 2 is : [12, 4, 6] The matrix after rows removal is : [[1, 10, 2], [5, 7, 9], [10, 1, 2]]
Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements.
Method #2: Using list comprehension
This is yet another way in which this task can be performed. In this, we perform a similar task in the shortened format using list comprehension in one line.
Python3
test_list1 = [[ 3 , 4 , 5 ], [ 2 , 6 , 8 ], [ 1 , 10 , 2 ], [ 5 , 7 , 9 ], [ 10 , 1 , 2 ]]
test_list2 = [ 12 , 4 , 6 ]
print ("The original list 1 is : " + str (test_list1))
print ("The original list 2 is : " + str (test_list2))
K = 1
res = [ele for ele in test_list1 if ele[K] not in test_list2]
print ("The matrix after rows removal is : " + str (res))
|
Output :
The original list 1 is : [[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]] The original list 2 is : [12, 4, 6] The matrix after rows removal is : [[1, 10, 2], [5, 7, 9], [10, 1, 2]]
Method #3: Here’s an approach using the filter function:
Python3
test_list1 = [[ 3 , 4 , 5 ], [ 2 , 6 , 8 ], [ 1 , 10 , 2 ], [ 5 , 7 , 9 ], [ 10 , 1 , 2 ]]
test_list2 = [ 12 , 4 , 6 ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
K = 1
res = list ( filter ( lambda x: x[K] not in test_list2, test_list1))
print ( "The matrix after rows removal is : " + str (res))
|
Output
The original list 1 is : [[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]]
The original list 2 is : [12, 4, 6]
The matrix after rows removal is : [[1, 10, 2], [5, 7, 9], [10, 1, 2]]
Time complexity: O(N) where n is the number of elements in test_list1.
Auxiliary Space: O(N) where n is the number of elements in res.
Explanation:
The filter function is applied to test_list1 using a lambda function that checks if the Kth element of each element in test_list1 is not in test_list2.
The result of filter is converted to a list and stored in res.
Method#4: Using the Recursive method
The algorithm for the recursive method remove_rows works as follows:
- If test_list1 is empty, return an empty list.
- If the Kth element of the first row of test_list1 is in test_list2, skip this row and call remove_rows recursively with the remaining rows of test_list1.
- Otherwise, keep this row and call remove_rows recursively with the remaining rows of test_list1.
Python3
def remove_rows(test_list1, test_list2, K):
if not test_list1:
return []
elif test_list1[ 0 ][K] in test_list2:
return remove_rows(test_list1[ 1 :], test_list2, K)
else :
return [test_list1[ 0 ]] + remove_rows(test_list1[ 1 :], test_list2, K)
test_list1 = [[ 3 , 4 , 5 ], [ 2 , 6 , 8 ], [ 1 , 10 , 2 ], [ 5 , 7 , 9 ], [ 10 , 1 , 2 ]]
test_list2 = [ 12 , 4 , 6 ]
print ( 'The original list 1 is :' + str (test_list1))
print ( 'The original list 2 is : ' + str (test_list2))
K = 1
res = remove_rows(test_list1, test_list2, K)
print ( 'The matrix after rows removal is : ' + str (res))
|
Output
The original list 1 is :[[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]]
The original list 2 is : [12, 4, 6]
The matrix after rows removal is : [[1, 10, 2], [5, 7, 9], [10, 1, 2]]
Time complexity: O(n*m), where n is the number of rows in test_list1 and m is the length of test_list2. This is because for each row in test_list1, we need to check if its Kth element is in test_list2, which takes O(m) time.
Auxiliary space: O(n), where n is the number of rows in test_list1. This is because we need to store a new list that contains all rows that are not removed.
Method 5: Using a generator expression with the if condition
- Initialize two lists, test_list1, and test_list2, which contain the list of lists and the values to be removed, respectively.
- Define a function remove_rows which takes three arguments: test_list1, test_list2, and K, where K specifies the index of the column to be checked for values to be removed.
- Inside the function, create a generator expression that loops through each list in test_list1 and checks if the value at index K is not in test_list2.
- If the condition is satisfied, yield the list from the generator expression.
- Convert the resulting generator expression to a list using the list() function and store it in the variable res.
- Return res as the output of the function.
- Initialize the value of K to 1.
- Call the remove_rows function with arguments test_list1, test_list2, and K and store the result in the variable res.
- Print the original lists and the resulting matrix after rows removal.
Python3
def remove_rows(test_list1, test_list2, K):
res = (lst for lst in test_list1 if lst[K] not in test_list2)
return list (res)
test_list1 = [[ 3 , 4 , 5 ], [ 2 , 6 , 8 ], [ 1 , 10 , 2 ], [ 5 , 7 , 9 ], [ 10 , 1 , 2 ]]
test_list2 = [ 12 , 4 , 6 ]
print ( 'The original list 1 is :' + str (test_list1))
print ( 'The original list 2 is : ' + str (test_list2))
K = 1
res = remove_rows(test_list1, test_list2, K)
print ( 'The matrix after rows removal is : ' + str (res))
|
Output
The original list 1 is :[[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]]
The original list 2 is : [12, 4, 6]
The matrix after rows removal is : [[1, 10, 2], [5, 7, 9], [10, 1, 2]]
Time complexity: O(N), where n is the number of elements in test_list1, since we iterate through each element of the list of lists once.
Auxiliary space: O(M), where m is the number of elements that pass the filter condition.
Similar Reads
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 Columns of Duplicate Elements
Given a Matrix, write a Python program to remove whole column if duplicate occurs in any row. Examples: Input : test_list = [[4, 3, 5, 2, 3], [6, 4, 2, 1, 1], [4, 3, 9, 3, 9], [5, 4, 3, 2, 1]] Output : [[4, 3, 5], [6, 4, 2], [4, 3, 9], [5, 4, 3]] Explanation : 1 has duplicate as next element hence 5
3 min read
Python | Remove Initial K column elements
Sometimes, while working with Matrix data, we can have stray elements 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
4 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 Record if Nth Column is K
Sometimes while working with a list of records, we can have a problem in which we need to perform the removal of records on the basis of the presence of certain elements at the Nth position of the record. Let us discuss certain ways in which this task can be performed. Method #1: Using loop This is
10 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 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 first K elements matching some condition
Removal of elements in list can be performed using many inbuilt functions. Removing all or just a single occurrence removal both functions are present in Python library. This article discusses to remove just the first K occurrences of elements matching particular condition. Method #1: Naive Method W
3 min read
Remove Kth Key from Dictionary - Python
We are given a dictionary we need to remove Kth key from the dictionary. For example, we are given a dictionary d = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3', 'key4': 'value4'} we need to remove the key2 so that the output should be {'key1': 'value1', 'key3': 'value3', 'key4': 'value4'}.
3 min read