Python - Remove front column from Matrix
Last Updated :
17 Apr, 2023
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))
OutputThe 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
OutputThe 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
OutputThe 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.
OutputThe 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).
Similar Reads
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 False row from matrix
Sometimes, while handling data, especially in the Machine Learning domain, we need to go through a lot of incomplete or empty data. We sometimes need to eliminate the rows which do not contain a value in any of the columns. Let's discuss certain ways to remove the rows that have all False values as
9 min read
Python | Remove Front K elements
We often come to situations in which we need to decrease the size of the list by truncating the first elements of the list. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar si
7 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 - Custom Columns Matrix
Sometimes, while working with Python lists, we can have a problem in which we need to extract certain columns from Matrix and recreate it. This kind of problem can have applications in data domains as they use Matrix as a prominent input parameter. Let's discuss certain ways in which this task can b
5 min read
Python - Rear column in Multi-sized Matrix
Given a Matrix with variable lengths rows, extract last column. Input : test_list = [[3, 4, 5], [7], [8, 4, 6], [10, 3]] Output : [5, 7, 6, 3] Explanation : Last elements of rows filtered. Input : test_list = [[3, 4, 5], [7], [8, 4, 6]] Output : [5, 7, 6] Explanation : Last elements of rows filtered
4 min read
Summation Matrix columns - Python
The task of summing the columns of a matrix in Python involves calculating the sum of each column in a 2D list or array. For example, given the matrix a = [[3, 7, 6], [1, 3, 5], [9, 3, 2]], the goal is to compute the sum of each column, resulting in [13, 13, 13]. Using numpy.sum()numpy.sum() is a hi
2 min read
Python | Get Kth Column of Matrix
Sometimes, while working with Python Matrix, one can have a problem in which one needs to find the Kth column of Matrix. This is a very popular problem in Machine Learning Domain and having solution to this is useful. Let's discuss certain ways in which this problem can be solved. Method #1 : Using
6 min read
Python | Remove duplicates in Matrix
While working with Python Matrix, we can face a problem in which we need to perform the removal of duplicates from Matrix. This problem can occur in Machine Learning domain because of extensive usage of matrices. Let's discuss certain way in which this task can be performed. Method : Using loop This
2 min read
Take Matrix input from user in Python
Matrix is nothing but a rectangular arrangement of data or numbers. In other words, it is a rectangular array of data or numbers. The horizontal entries in a matrix are called as 'rows' while the vertical entries are called as 'columns'. If a matrix has r number of rows and c number of columns then
5 min read