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 ?

sublists = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]]
print("Given List:")
print(sublists)
print("\nLast Items from sublists:")

for item in sublists:
    print(item[-1])
Given List:
[['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]]

Last Items from sublists:
1
Fri
7

Using List Comprehension

A more concise approach is to use list comprehension to extract the last element from each sublist in one line ?

sublists = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]]
print("Given List:")
print(sublists)

last_elements = [item[-1] for item in sublists]
print("\nLast Items from sublists:")
print(last_elements)
Given List:
[['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]]

Last Items from sublists:
[1, 'Fri', 7]

Using zip and *

The * operator allows us to unpack the sublists and give access to individual elements. We can use * with reversed sublists to access the element at index 0 from each reversed sublist (which is the last element of the original sublist) ?

sublists = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]]
print("Given List:")
print(sublists)

last_elements = list(list(zip(*map(reversed, sublists)))[0])
print("\nLast Items from sublists:")
print(last_elements)
Given List:
[['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]]

Last Items from sublists:
[1, 'Fri', 7]

Using itemgetter

The itemgetter(-1) constructs a callable that takes an iterable object like dictionary, list, tuple etc. as input, and fetches the last element out of it using index -1 ?

from operator import itemgetter

sublists = [['Mon', 1], ['Tue', 'Wed', "Fri"], [12, 3, 7]]
print("Given List:")
print(sublists)

last_elements = list(map(itemgetter(-1), sublists))
print("\nLast Items from sublists:")
print(last_elements)
Given List:
[['Mon', 1], ['Tue', 'Wed', 'Fri'], [12, 3, 7]]

Last Items from sublists:
[1, 'Fri', 7]

Comparison

Method Readability Performance Best For
For Loop High Good Beginners, complex processing
List Comprehension High Best Simple operations, Pythonic code
zip and * Low Good Advanced use cases
itemgetter Medium Good Functional programming style

Conclusion

List comprehension is the most Pythonic and efficient approach for extracting last elements from sublists. For beginners, the for loop method provides clarity and ease of understanding.

Updated on: 2026-03-15T18:30:50+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements