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
# Python3 code to demonstrate working of
# Remove last element from each row in 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 last element from each row in Matrix
# Using loop + del + list slicing
for ele in test_list:
del ele[-1]
# printing result
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
# Python3 code to demonstrate working of
# Remove last element from each row in 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 last element from each row in Matrix
# Using list comprehension + list slicing
res = [ele[:-1] for ele in test_list]
# printing result
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
# Python3 code to demonstrate working of
# Remove last element from each row in 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 last element from each row in Matrix
res=[]
for i in test_list:
i.pop()
res.append(i)
# printing result
print("Matrix after removal of rear element from rows : " + str(res))
OutputThe 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
# Python3 code to demonstrate working of
# Remove last element from each row in Matrix
# Using map() function
# initialize list
test_list = [[1, 3, 4], [2, 4, 6], [3, 8, 1]]
# printing original list
print("The original list : " + str(test_list))
# Remove last element from each row in Matrix
# Using map() function
res = list(map(lambda x: x[:-1], test_list))
# printing result
print("Matrix after removal of rear element from rows : " + 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 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
# Python3 code to demonstrate working of
# Remove last element from each row in Matrix
def remove_last_elem_recursive(matrix, i=0):
if i == len(matrix):
return
del matrix[i][-1]
remove_last_elem_recursive(matrix, i+1)
# initialize list
test_list = [[1, 3, 4], [2, 4, 6], [3, 8, 1]]
# printing original list
print("The original list : " + str(test_list))
# Remove last element from each row in Matrix
remove_last_elem_recursive(test_list)
# printing result
print("Matrix after removal of rear element from rows : " + str(test_list))
OutputThe 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.Pythona = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remove = [20, 40,
2 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 | Remove given element from the list
Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 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
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 rear element from list
A stack data structure is a very well-known data structure, lists in Python usually append the elements to the end of the list. For implementing a stack data structure, it is essential to be able to remove the end element from a list. Letâs discuss the ways to achieve this so that stack data structu
6 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
Python - List Elements Grouping in Matrix
Given a Matrix, for groups according to list elements, i.e each group should contain all elements from List. Input : test_list = [[2, 6], [7, 8], [1, 4]], check_list = [1, 2, 4, 6] Output : [[7, 8], [[1, 2], [4, 6]]] Explanation : 1, 2, 4, 6 elements rows are grouped. Input : test_list = [[2, 7], [7
8 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