Open In App

Python – Custom Rows Removal depending on Kth Column

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# Custom Rows Removal depending on Kth Column
# using loop
 
# Initializing lists
test_list1 = [[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]]
test_list2 = [12, 4, 6]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Initializing K
K = 1
 
# Custom Rows Removal depending on Kth Column
# using loop
res = []
for ele in test_list1:
  if ele[K] not in test_list2:
    res.append(ele)
             
# printing result
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




# Python3 code to demonstrate
# Custom Rows Removal depending on Kth Column
# using list comprehension
 
# Initializing lists
test_list1 = [[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]]
test_list2 = [12, 4, 6]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Initializing K
K = 1
 
# Custom Rows Removal depending on Kth Column
# using list comprehension
res = [ele for ele in test_list1 if ele[K] not in test_list2]
             
# printing result
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




# Python3 code to demonstrate
# Custom Rows Removal depending on Kth Column
# using filter function
   
# Initializing lists
test_list1 = [[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]]
test_list2 = [12, 4, 6]
   
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
   
# Initializing K
K = 1
   
# Custom Rows Removal depending on Kth Column
# using filter function
res = list(filter(lambda x: x[K] not in test_list2, test_list1))
               
# printing result
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:

  1. If test_list1 is empty, return an empty list.
  2. 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.
  3. Otherwise, keep this row and call remove_rows recursively with the remaining rows of test_list1.

Python3




# Python3 code to demonstrate
# Custom Rows Removal depending on Kth Column
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)
 
 
# Initializing lists
test_list1 = [[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]]
test_list2 = [12, 4, 6]
 
# Printing original lists
print('The original list 1 is :' + str(test_list1))
print('The original list 2 is : ' + str(test_list2))
 
# Initializing K
K = 1
 
# Custom Rows Removal depending on Kth Column
res = remove_rows(test_list1, test_list2, K)
 
# Printing result
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

  1. Initialize two lists, test_list1, and test_list2, which contain the list of lists and the values to be removed, respectively.
  2. 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.
  3. 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.
  4. If the condition is satisfied, yield the list from the generator expression.
  5. Convert the resulting generator expression to a list using the list() function and store it in the variable res.
  6. Return res as the output of the function.
  7. Initialize the value of K to 1.
  8. Call the remove_rows function with arguments test_list1, test_list2, and K and store the result in the variable res.
  9. 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)
 
 
# Initializing lists
test_list1 = [[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]]
test_list2 = [12, 4, 6]
 
# Printing original lists
print('The original list 1 is :' + str(test_list1))
print('The original list 2 is : ' + str(test_list2))
 
# Initializing K
K = 1
 
# Custom Rows Removal depending on Kth Column
res = remove_rows(test_list1, test_list2, K)
 
# Printing result
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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads