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 and 5th rows are removed.
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, 3, 5]
Output: [[3, 5, 2], [5, 3, 1], [9, 4, 1], [0, 1, 2]]
Explanation: 1st, 3rd and 5th rows are removed.
Method #1 : Using loop + pop() + back iteration
In this, removal is handled using pop(), and back iteration is necessary to make sure due to rearrangement at deletion, the wrong positional row is not removed.
Python3
test_list = [[ 3 , 5 , 2 ], [ 1 , 8 , 9 ], [ 5 , 3 , 1 ],
[ 0 , 1 , 6 ], [ 9 , 4 , 1 ], [ 1 , 2 , 10 ],
[ 0 , 1 , 2 ]]
print ( "The original list is: " + str (test_list))
idx_lis = [ 1 , 2 , 5 ]
for idx in idx_lis[:: - 1 ]:
test_list.pop(idx)
print ( "Matrix after removal: " + str (test_list))
|
Output:
The original list is: [[3, 5, 2], [1, 8, 9], [5, 3, 1], [0, 1, 6], [9, 4, 1], [1, 2, 10], [0, 1, 2]]
Matrix after removal: [[3, 5, 2], [0, 1, 6], [9, 4, 1], [0, 1, 2]]
Time Complexity: O(n*m)
Auxiliary Space: O(k)
Method #2 : Using enumerate() + list comprehension
In this, rather than removing rows by index, we perform the task of only adding rows that are not part of removing the index list.
Python3
test_list = [[ 3 , 5 , 2 ], [ 1 , 8 , 9 ], [ 5 , 3 , 1 ],
[ 0 , 1 , 6 ], [ 9 , 4 , 1 ], [ 1 , 2 , 10 ],
[ 0 , 1 , 2 ]]
print ( "The original list is: " + str (test_list))
idx_lis = [ 1 , 2 , 5 ]
res = [sub[ 1 ] for sub in enumerate (test_list) if sub[ 0 ] not in idx_lis]
print ( "Matrix after removal: " + str (res))
|
Output:
The original list is: [[3, 5, 2], [1, 8, 9], [5, 3, 1], [0, 1, 6], [9, 4, 1], [1, 2, 10], [0, 1, 2]]
Matrix after removal: [[3, 5, 2], [0, 1, 6], [9, 4, 1], [0, 1, 2]]
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 the filter() function with lambda function to remove rows based on the given indices.
Step-by-step approach:
- Define the list of lists.
- Define the list of indices to be removed.
- Use the filter() function along with the lambda function to remove the rows from the list based on the given indices.
- Convert the filtered object into a list.
- Print the resulting list.
Below is the implementation of the above approach:
Python3
test_list = [[ 3 , 5 , 2 ], [ 1 , 8 , 9 ], [ 5 , 3 , 1 ],
[ 0 , 1 , 6 ], [ 9 , 4 , 1 ], [ 1 , 2 , 10 ],
[ 0 , 1 , 2 ]]
print ( "The original list is: " + str (test_list))
idx_lis = [ 1 , 2 , 5 ]
res = list ( filter ( lambda x: test_list.index(x) not in idx_lis, test_list))
print ( "Matrix after removal: " + str (res))
|
Output
The original list is: [[3, 5, 2], [1, 8, 9], [5, 3, 1], [0, 1, 6], [9, 4, 1], [1, 2, 10], [0, 1, 2]]
Matrix after removal: [[3, 5, 2], [0, 1, 6], [9, 4, 1], [0, 1, 2]]
Time complexity: O(n) where n is the number of rows in the list of lists.
Auxiliary space: O(1) as we are not using any extra space to store the intermediate results.
Method 4: Using numpy.delete() function
Another alternative approach to remove positional rows from a list is by using the numpy.delete() function. The numpy.delete() function is a built-in function of the numpy library that can delete the specified subarrays along the given axis from the input array.
Here’s the step-by-step approach:
- Import the numpy library.
- Initialize the list as a numpy array.
- Use the numpy.delete() function to remove the subarrays with the given indices to remove along the first axis (axis=0).
- Convert the resulting numpy array back to a list.
- Print the result.
Python3
import numpy as np
test_list = [[ 3 , 5 , 2 ], [ 1 , 8 , 9 ], [ 5 , 3 , 1 ],
[ 0 , 1 , 6 ], [ 9 , 4 , 1 ], [ 1 , 2 , 10 ],
[ 0 , 1 , 2 ]]
print ( "The original list is: " + str (test_list))
idx_lis = [ 1 , 2 , 5 ]
test_arr = np.array(test_list)
test_arr = np.delete(test_arr, idx_lis, axis = 0 )
test_list = test_arr.tolist()
print ( "Matrix after removal: " + str (test_list))
|
OUTPUT:
The original list is: [[3, 5, 2], [1, 8, 9], [5, 3, 1], [0, 1, 6], [9, 4, 1], [1, 2, 10], [0, 1, 2]]
Matrix after removal: [[3, 5, 2], [0, 1, 6], [9, 4, 1], [0, 1, 2]]
Time complexity: O(n), where n is the length of the original list.
Auxiliary space: O(n), since a new numpy array is created and then converted back to a list
Method #5: Using a boolean mask with a numpy array
- The program imports the numpy library and assigns it to the alias “np”.
- A 2-dimensional list “test_list” is defined as containing 7 sublists, each containing 3 integer elements.
- Another list “idx_lis” is defined as containing 3 integer elements.
- The numpy array is created by passing the “test_list” to the np.array() function.
- A boolean mask is created using the np.ones() function to create an array of all True values, with the same length as “test_list”. Then the elements of the “idx_lis” are set to False in the mask array to mark the rows to be removed.
- The boolean mask is then used to select the rows to keep from the numpy array by using the mask as an index. This is done by passing the mask array as the index to the numpy array “arr”, resulting in a reduced array “res”.
- The resulting array “res” is converted back to a list by using the tolist() method of the numpy array object.
- Finally, the result list “res_list” is printed to the console as a string.
Below is the implementation of the above approach:
Python3
import numpy as np
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 ]
arr = np.array(test_list)
mask = np.ones( len (test_list), dtype = bool )
mask[idx_lis] = False
res = arr[mask]
res_list = res.tolist()
print ( "Matrix after removal: " + str (res_list))
|
Output:
Matrix after removal: [[3, 5, 2], [0, 1, 6], [9, 4, 1], [0, 1, 2]]
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
07 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...