
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Get last element of each sublist in Python
A list in python can also contain lists inside it as elements. These nested lists are called sublists. In this article we will solve the challenge of retrieving only the last element of each sublist in a given list.
Using for loop
It is a very simple approach in which we loop through the sublists fetching the item at index -1 in them. A for loop is used for this purpose as shown below.
Example
Alist = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12,3,7]] print("Given List:\n",Alist) print("Lastst Items from sublists:\n") for item in Alist: print((item[-1]))
Output
Running the above code gives us the following result −
Given List: [['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]] First Items from sublists: 1 Fri 7
Using zip and *
The * allows us to unpack the sublists and give access to individual elements of the sublist. So in this case we will use * and reversed list to access the element at index 0 from each element. Then we finally zip the result to get a list of the first element from the sublists.
Example
Alist = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12,3,7]] print("Given List:\n",Alist) print("\n First Items from sublists:\n") print(list(list(zip(*map(reversed,Alist)))[0]))
Output
Running the above code gives us the following result −
Given List: [['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]] First Items from sublists: [1, 'Fri', 7]
Using itemgetter
The itemgetter(i) constructs a callable that takes an iterable object like dictionary,list, tuple etc. as input, and fetches the i-th element out of it. So we can use this method to get the first items of the list using the map function as follows.
Example
from operator import itemgetter Alist = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12,3,7]] print("Given List:\n",Alist) print("\n First Items from sublists:\n") print(list(map(itemgetter(-1), Alist)))
Output
Running the above code gives us the following result −
Given List: [['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]] First Items from sublists: [1, 'Fri', 7]
- Related Articles
- Get first element of each sublist in Python
- Python How to get the last element of list
- How to get the last element of a list in Python?
- Python Getting sublist element till N
- Find maximum value in each sublist in Python
- How to get the second-to-last element of a list in Python?
- How to get last element in android ConcurrentLinkedDeque?
- Get the element ordered last in Java TreeSet
- How to get the last element in JavaScript array?
- Program to find a sublist where first and last values are same in Python
- How to get sublist of List in Java?
- Last occurrence of some element in a list in Python
- Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple using Python program
- Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
- Get SubList from LinkedList in Java
