Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Uneven Sized Matrix Column Minimum
Next article icon

Python – Rear column in Multi-sized Matrix

Last Updated : 01 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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




# 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




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

  • 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




# 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




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



Next Article
Python - Uneven Sized Matrix Column Minimum
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences