5 Best Ways to Get the Last Element of Each Sublist in Python

Rate this post

πŸ’‘ Problem Formulation: When dealing with lists of lists in Python, it’s a common task to retrieve the last element from each inner list. For instance, given a list of lists like [[1, 2, 3], [4, 5, 6], [7, 8, 9]], the goal is to extract the last elements resulting in [3, 6, 9].

Method 1: Using a for Loop

This method iterates over each sublist in the parent list using a straightforward for loop, and accesses the last element with index -1 which is the Pythonic way to get the last item of a list.

Here’s an example:

sublists = [[1, 'a', 3], [4, 'b', 6], [7, 'c', 9]]
last_elements = []
for sublist in sublists:
    last_elements.append(sublist[-1])

Output: [3, 6, 9]

This code snippet creates an empty list called last_elements and iterates over each sublist in sublists, appending the last element of each sublist to the last_elements list. This method is simple and easy to understand but might not be the most concise way to perform this task.

Method 2: List Comprehension

List comprehension provides a more concise way to create lists. This method uses list comprehension to build the list of last elements in a single line of code.

Here’s an example:

sublists = [[1, 'a', 3], [4, 'b', 6], [7, 'c', 9]]
last_elements = [sublist[-1] for sublist in sublists]

Output: [3, 6, 9]

Here, the list comprehension iterates over each sublist and takes the last element, building a new list with these items. This method is compact and Pythonic, often preferred for its brevity and readability for simple operations like this one.

Method 3: Using the map and lambda Functions

The map function applies a given function to every item of an iterable. Combined with a lambda function, which defines a simple one-line function, this method can be used to extract the last elements of sublists.

Here’s an example:

sublists = [[1, 'a', 3], [4, 'b', 6], [7, 'c', 9]]
last_elements = list(map(lambda x: x[-1], sublists))

Output: [3, 6, 9]

This snippet uses map() to apply a lambda function to each sublist in sublists, which extracts the last element. The result is then converted back into a list. map can be useful when the operation is complex, but here it might be overkill for such a simple task and a bit less readable than list comprehension.

Method 4: Using a Function

Defining a custom function for retrieving the last element of each sublist can improve the readability and reusability of your code. This can be especially useful if the logic for getting the last element might change or is more complex than a simple index access.

Here’s an example:

def get_last_elements(list_of_lists):
    return [sublist[-1] for sublist in list_of_lists]

sublists = [[1, 'a', 3], [4, 'b', 6], [7, 'c', 9]]
last_elements = get_last_elements(sublists)

Output: [3, 6, 9]

The get_last_elements function encapsulates the logic for extracting the last element from each sublist using list comprehension. Then the function is called with sublists, and the results are stored in last_elements. This method is clean and modular, making the code easier to test and maintain.

Bonus One-Liner Method 5: Using itemgetter

Python’s itemgetter function from the operator module can be used to create a callable that accesses the last item of a list, which can then be mapped to each sublist.

Here’s an example:

from operator import itemgetter
sublists = [[1, 'a', 3], [4, 'b', 6], [7, 'c', 9]]
last_elements = list(map(itemgetter(-1), sublists))

Output: [3, 6, 9]

The code uses itemgetter(-1) to create a function that gets the last element of its argument, then applies that function to each sublist. This method is efficient and elegant, but the use of map and itemgetter together can be less readable for Python beginners.

Summary/Discussion

  • Method 1: For Loop. Straightforward and easy to understand. However, it’s not the most concise way to solve the problem.
  • Method 2: List Comprehension. More concise and Pythonic than a for loop. It’s the most common and recommended way for simple tasks like this one.
  • Method 3: map and lambda. Suitable for more complex operations, but overkill and less readable for such simple cases.
  • Method 4: Using a Function. Enhances readability and is easily reusable. Ideal when the logic for retrieving the last element might vary or become more complex.
  • Method 5: Using itemgetter. Efficient and compact but potentially confusing for those not familiar with the operator module.