Open In App

Python – Rear column in Multi-sized Matrix

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 code to demonstrate working of
# Rear column in Multisized Matrix
# Using loop
 
# initializing list
test_list = [[3, 4, 5], [7], [8, 4, 6, 1], [10, 3]]
 
# printing original list
print("The original list is : " + str(test_list))
 
res = []
for sub in test_list:
     
    # getting rear element using "-1"
    res.append(sub[-1])
 
# printing results
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 code to demonstrate working of
# Rear column in Multisized Matrix
# Using list comprehension
 
# initializing list
test_list = [[3, 4, 5], [7], [8, 4, 6, 1], [10, 3]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# one-liner to solve this problem
res = [sub[-1] for sub in test_list]
 
# printing results
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:




# Python3 code to demonstrate working of
# Rear column in Multisized Matrix
# Using map function
 
# initializing list
test_list = [[3, 4, 5], [7], [8, 4, 6, 1], [10, 3]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# define a function to get the last element of a list
def get_last_element(sub):
    return sub[-1]
 
# apply the get_last_element function to each sublist using map
res = list(map(get_last_element, test_list))
 
# printing results
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 code to demonstrate working of
# Rear column in Multisized Matrix
# Using list slicing
 
# initializing list
test_list = [[3, 4, 5], [7], [8, 4, 6, 1], [10, 3]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# get the last element of each sublist using list slicing
res = [sublist[-1] for sublist in test_list]
 
# printing results
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.


Article Tags :