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.
Share your thoughts in the comments
Please Login to comment...