Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Remove last element from each row in Matrix

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


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




# 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


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




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


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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments