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 of the above functionalities can be used to perform this task. In this, we run a loop for each row in the matrix and remove the front element using del.
Python3
# Python3 code to demonstrate working of # Remove front column from Matrix # Using loop + del + list slicing # initialize list test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]] # printing original list print ( "The original list : " + str (test_list)) # Remove front column from Matrix # Using loop + del + list slicing for ele in test_list: del ele[ 0 ] # printing result print ( "Matrix after removal of front column : " + str (test_list)) |
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]] Matrix after removal of front column : [[3, 4], [4, 6], [8, 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 + list slicing
The combination of above functions can also be used to perform this task. In this, we just iterate for each row and delete front element using list slicing.
Python3
# Python3 code to demonstrate working of # Remove front column from Matrix # Using list comprehension + list slicing # initialize list test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]] # printing original list print ( "The original list : " + str (test_list)) # Remove front column from Matrix # Using list comprehension + list slicing res = [ele[ 1 :] for ele in test_list] # printing result print ( "Matrix after removal of front column : " + str (res)) |
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]] Matrix after removal of front column : [[3, 4], [4, 6], [8, 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 pop() method
Python3
# Python3 code to demonstrate working of # Remove front column from Matrix # initialize list test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]] # printing original list print ( "The original list : " + str (test_list)) # Remove front column from Matrix res = [] for i in test_list: i.pop( 0 ) res.append(i) # printing result print ( "Matrix after removal of front column : " + str (res)) |
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]] Matrix after removal of front column : [[3, 4], [4, 6], [8, 1]]
Method #4 : Using zip() and list slicing
STEP BY STEP APPROACH :
- Initialize a 2D list test_list with some integer values.
- Print the original list using the print() function and the str() method to convert the list to a string.
- Use map() function with a lambda function to remove the first column from each row of the 2D list. The lambda function takes a row as input, and returns a new list that starts from the second element of the row. The map() function applies this lambda function to each row of the 2D list.
- Convert the resulting map object to a list using the list() function.
- Assign the resulting list to a variable res.
- Print the modified 2D list using the print() function and the str() method to convert the list to a string.
Python3
# Python3 code to demonstrate working of # Remove front column from Matrix # Using zip() and list slicing # initialize list test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]] # printing original list print ( "The original list : " + str (test_list)) # Remove front column from Matrix # Using zip() and list slicing res = list ( map ( lambda x:x[ 1 :], test_list)) # printing result print ( "Matrix after removal of front column : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]] Matrix after removal of front column : [[3, 4], [4, 6], [8, 1]]
Time Complexity: O(N), where N is the length of the matrix.
Auxiliary Space: O(1)
Method #5: Using map() and lambda():
Python3
# Initialize the matrix test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]] res = list ( map ( lambda x: x[ 1 :], test_list)) # printing original list print ( "The original list : " + str (test_list)) print ( "Matrix after removal of front column :" , res) #This code is contributed by pinjala Jyothi |
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]] Matrix after removal of front column : [[3, 4], [4, 6], [8, 1]]
Time Complexity: O(N)
Auxiliary Space: O(N)
Method #6:Using reduce()
Algorithm
- Start with the given matrix M.
- Create an empty matrix N of the same dimensions as M.
- For each row i in M:
a. Create a new row R by removing the first element of i.
b. Append R to N. - Return N as the resulting matrix with the first column removed.
Python3
from functools import reduce test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]] res = reduce ( lambda acc, curr: acc + [curr[ 1 :]], test_list, []) print ( "The original list : " + str (test_list)) print ( "Matrix after removal of front column :" , res) #THis code is contributed by Vinay Pinjala. |
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]] Matrix after removal of front column : [[3, 4], [4, 6], [8, 1]]
Time complexity: O(nm), where n is the number of rows and m is the number of columns in the matrix.
The algorithm iterates over each row of the matrix once, and for each row it creates a new row with one less element by removing the first element. The time complexity of creating a new row is O(m), since it involves copying m-1 elements. Therefore, the overall time complexity is O(nm).
Space complexity: O(nm), where n is the number of rows and m is the number of columns in the matrix.
The algorithm creates a new matrix of the same dimensions as the input matrix, which requires O(nm) space. Additionally, the algorithm creates a new row for each row in the input matrix, which requires O(nm) additional space. Therefore, the overall space complexity is O(nm).