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 | Search in Nth Column of Matrix
Sometimes, while working with Python Matrix, we can have a problem in which we need to check if an element is present or not. This problem is simpler, but a variation to it can be to perform a check in a particular column of Matrix. Let's discuss a shorthand by which this task can be performed. Meth
10 min read
Python | Nth Column vertical string in Matrix
Sometimes, while working with Python Matrix, we can have a problem in which we need to access the Matrix in vertical form and extract strings from the same, that too as a string, not merely as a list of characters. This task has its application in gaming in which we need to extract strings during cr
7 min read
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
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
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
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 - Pair elements with Rear element in Matrix Row
Sometimes, while working with data, we can have a problem in which we need to pair each element in container to a specific index element, like rear element. This kind of problem can have application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using lis
7 min read