Python – Rear column in Multi-sized Matrix
Last Updated :
01 May, 2023
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.
Method #1: Using loop
This is brute way to solve this, we access last element using “-1”, iterate for each row.
Python3
test_list = [[ 3 , 4 , 5 ], [ 7 ], [ 8 , 4 , 6 , 1 ], [ 10 , 3 ]]
print ( "The original list is : " + str (test_list))
res = []
for sub in test_list:
res.append(sub[ - 1 ])
print ( "Filtered column : " + str (res))
|
Output
The original list is : [[3, 4, 5], [7], [8, 4, 6, 1], [10, 3]]
Filtered column : [5, 7, 1, 3]
Time Complexity: O(n), where n is the length of the input list.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #2: Using list comprehension
This is another way to solve this, in this, we perform above task in similar way, just as a shorthand.
Python3
test_list = [[ 3 , 4 , 5 ], [ 7 ], [ 8 , 4 , 6 , 1 ], [ 10 , 3 ]]
print ( "The original list is : " + str (test_list))
res = [sub[ - 1 ] for sub in test_list]
print ( "Filtered column : " + str (res))
|
Output
The original list is : [[3, 4, 5], [7], [8, 4, 6, 1], [10, 3]]
Filtered column : [5, 7, 1, 3]
Method 3: Using the built-in map() function.
Here’s the step-by-step approach:
- Initialize the matrix list test_list.
- Define a function get_last_element() that takes a list and returns its last element using -1 index.
- Use map() function to apply the get_last_element() function on each sublist of test_list.
- Convert the map object to a list and store it in variable res.
- Print the filtered column.
Python3
test_list = [[ 3 , 4 , 5 ], [ 7 ], [ 8 , 4 , 6 , 1 ], [ 10 , 3 ]]
print ( "The original list is : " + str (test_list))
def get_last_element(sub):
return sub[ - 1 ]
res = list ( map (get_last_element, test_list))
print ( "Filtered column : " + str (res))
|
Output
The original list is : [[3, 4, 5], [7], [8, 4, 6, 1], [10, 3]]
Filtered column : [5, 7, 1, 3]
Time complexity: O(n), where n is the total number of elements in the matrix list test_list.
Auxiliary space: O(n), since we are storing the filtered column in a new list res.
Method 4: Using list slicing
This method uses list comprehension to iterate over the sublists in test_list and retrieve the last element of each sublist using list slicing. It is a concise and efficient way of achieving the same result as the previous methods.
Python3
test_list = [[ 3 , 4 , 5 ], [ 7 ], [ 8 , 4 , 6 , 1 ], [ 10 , 3 ]]
print ( "The original list is : " + str (test_list))
res = [sublist[ - 1 ] for sublist in test_list]
print ( "Filtered column : " + str (res))
|
Output
The original list is : [[3, 4, 5], [7], [8, 4, 6, 1], [10, 3]]
Filtered column : [5, 7, 1, 3]
Time complexity: O(n), where n is the number of elements in the input list.
Auxiliary space: O(n), where n is the number of elements in the input list, as the output list has the same number of elements as the input list.
Similar Reads
Python - Uneven Sized Matrix Column Minimum
The usual list of list, unlike conventional C type Matrix, can allow the nested list of lists with variable lengths, and when we require the minimum of its columns, the uneven length of rows may lead to some elements in that elements to be absent and if not handled correctly, may throw exception. Le
7 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 | Row with Minimum element in Matrix
We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but sometimes we need to print the entire row containing it and having shorthands to perform the same are always helpful as this kind of problem can come
5 min read
Python - Summation of kth column in a matrix
Sometimes, while working with Python Matrix, we may have a problem in which we require to find the summation of a particular column. This can have a possible application in day-day programming and competitive programming. Letâs discuss certain ways in which this task can be performed. Method #1 : Us
8 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
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
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
Python - Reverse sort Matrix Row by Kth Column
Sometimes, while working with data, we can have a problem in which we need to perform sorting of each row of records by some of decisive factor like score. This kind of problem is common in competitive programming and web development. Lets discuss certain ways in which this task can be performed. Me
4 min read
Python | Nth column Matrix Product
Sometimes, while working with Python Matrix, we may have a problem in which we require to find the product of a particular column. This can have a possible application in day-day programming and competitive programming. Letâs discuss certain ways in which this task can be performed. Method #1: Using
7 min read
Python | Matrix Tuple pair Column product
Sometimes, we encounter a problem where we deal with a complex type of matrix column product in which we are given a tuple and we need to perform the product of its like elements. This has a good application in Machine Learning domain. Letâs discuss certain ways in which this can be done. Method #1
4 min read