Python | Remove last element from each row in Matrix
Last Updated :
12 Apr, 2023
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 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 rear element using del.
Python3
test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]]
print ( "The original list : " + str (test_list))
for ele in test_list:
del ele[ - 1 ]
print ( "Matrix after removal of rear element from rows : " + str (test_list))
|
Output :
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]]
Matrix after removal of rear element from rows : [[1, 3], [2, 4], [3, 8]]
Time complexity: O(n*m).
Auxiliary space: O(1).
Method #2: Using list comprehension + list slicing The combination of the above functions can also be used to perform this task. In this, we just iterate for each row and delete the rear element using list slicing.
Python3
test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]]
print ( "The original list : " + str (test_list))
res = [ele[: - 1 ] for ele in test_list]
print ( "Matrix after removal of rear element from rows : " + str (res))
|
Output :
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]]
Matrix after removal of rear element from rows : [[1, 3], [2, 4], [3, 8]]
Time complexity: O(nm), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary space: O(nm), as a new matrix is created to store the modified rows.
Method #3 : Using pop() method
Python3
test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]]
print ( "The original list : " + str (test_list))
res = []
for i in test_list:
i.pop()
res.append(i)
print ( "Matrix after removal of rear element from rows : " + str (res))
|
Output
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]]
Matrix after removal of rear element from rows : [[1, 3], [2, 4], [3, 8]]
Time complexity: O(nm), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary space: O(nm), as a new matrix is created to store the modified rows.
Method #4: Using map() function
The code uses the map() function to remove the last element from each row in the matrix. map() function applies a given function to all the items of an input_list, and returns a list containing all the results.
Here, the lambda function lambda x: x[:-1] is applied to each row of the matrix (which is represented by a nested list) using map() function. The lambda function takes a single argument x, which is a row of the matrix and returns the same row but with the last element removed, by using slicing x[:-1]. This way, all the rows of the matrix are processed by the lambda function and a new list is returned by the map() function, which contains all the rows with the last element removed.
The time complexity of this method is O(n), where n is the total number of elements in the matrix. This is because the map() function iterates over all the elements of the matrix and applies the lambda function to each element, which takes constant time.
The Auxiliary space of this method is O(n) where n is the total number of elements in the matrix. This is because a new list is returned by the map() function, which is equal to the size of the input matrix.
Python3
test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]]
print ( "The original list : " + str (test_list))
res = list ( map ( lambda x: x[: - 1 ], test_list))
print ( "Matrix after removal of rear element from rows : " + str (res))
|
Output
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]]
Matrix after removal of rear element from rows : [[1, 3], [2, 4], [3, 8]]
Method#5: Using Recursive method.
The remove_last_elem_recursive function takes a matrix as input, along with an optional index variable i (which is initially set to 0). The function removes the last element from the row at index i and then recursively calls itself with an updated value of i.
Python3
def remove_last_elem_recursive(matrix, i = 0 ):
if i = = len (matrix):
return
del matrix[i][ - 1 ]
remove_last_elem_recursive(matrix, i + 1 )
test_list = [[ 1 , 3 , 4 ], [ 2 , 4 , 6 ], [ 3 , 8 , 1 ]]
print ( "The original list : " + str (test_list))
remove_last_elem_recursive(test_list)
print ( "Matrix after removal of rear element from rows : " + str (test_list))
|
Output
The original list : [[1, 3, 4], [2, 4, 6], [3, 8, 1]]
Matrix after removal of rear element from rows : [[1, 3], [2, 4], [3, 8]]
The time complexity of this recursive method is also O(n), where n is the total number of elements in the matrix. This is because the function visits each element of the matrix exactly once.
The auxiliary space is O(n) as well, because the function modifies the matrix in place and does not create any additional data structures.
Similar Reads
Remove Multiple Elements from List in Python
In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list. [GFGTABS] Python a = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remov
3 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 column from Matrix
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 o
6 min read
Python program to remove last element from set
Given a set, the task is to write a Python program to delete the last element from the set. Example: Input: {1,2,3,4}Remove 4Output: {1,2,3} Input: {"Geeks","For"}Remove GeeksOutput: {"For"}Explanation: The idea is to remove the last element from the set 4 in the first case and "Geeks" in the second
2 min read
Python | Remove given element from list of lists
The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this p
6 min read
Python program to Remove the last element from list
Given a list of numbers or strings, the task is to write a Python program to remove the last element from a list. Example: Input : [12,53,11,76,22,21] Output : [12,53,11,76,22] Input : ["abc","gfg","text","python"] Output : ["abc","gfg","text"] The application of this type of problem in day-day prog
3 min read
Python program to remove last element from Tuple
Given a tuple, the task is to write a Python program to delete the last element in the tuple in Python. Example: Input: ("geeks", "for", "geeks") Output:("geeks", "for") Explanation: Here we are deleting the last element of the tuple and finally modifying the original one. Note: Tuples are immutable
3 min read
Remove an Element from a List by Index in Python
We are given a list and an index value. We have to remove an element from a list that is present at that index value in a list in Python. In this article, we will see how we can remove an element from a list by using the index value in Python. Example: Input: [1, 2, 3, 4, 5], index = 2 Output: [1, 2
3 min read
Python | Remove similar element rows in tuple Matrix
Sometimes, while working with data, we can have a problem in which we need to remove elements from the tuple matrix on a condition that if all elements in row of tuple matrix is same. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + all() This ta
6 min read
Search Elements in a Matrix - Python
The task of searching for elements in a matrix in Python involves checking if a specific value exists within a 2D list or array. The goal is to efficiently determine whether the desired element is present in any row or column of the matrix. For example, given a matrix a = [[4, 5, 6], [10, 2, 13], [1
3 min read