Open In App

Python – Remove front column from Matrix

Improve
Improve
Like Article
Like
Save
Share
Report

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))


Output : 

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))


Output : 

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))


Output

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 :

  1. Initialize a 2D list test_list with some integer values.
  2. Print the original list using the print() function and the str() method to convert the list to a string.
  3. 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.
  4. Convert the resulting map object to a list using the list() function.
  5. Assign the resulting list to a variable res.
  6. 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


Output

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


Output

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 

  1. Start with the given matrix M.
  2. Create an empty matrix N of the same dimensions as M.
  3. For each row i in M:
    a. Create a new row R by removing the first element of i.
    b. Append R to N.
  4. 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.


Output

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).



Last Updated : 17 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads