Wednesday, December 25, 2024
Google search engine
HomeLanguagesPython | Get last element of each sublist

Python | Get last element of each sublist

Given a list of lists, write a Python program to extract the last element of each sublist in the given list of lists. 

Examples:

Input : [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
Output : [3, 5, 9]

Input : [['x', 'y', 'z'], ['m'], ['a', 'b'], ['u', 'v']]
Output : ['z', 'm', 'b', 'v']

  Approach #1 : List comprehension 

Python3




# Python3 program to extract first and last
# element of each sublist in a list of lists
 
def Extract(lst):
    return [item[-1] for item in lst]
     
# Driver code
lst = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
print(Extract(lst))


Output:

[3, 5, 9]

Time complexity: O(n), where n is the total number of elements in all sublists.
Auxiliary space: O(n), as the program creates a new list to store the extracted last elements of each sublist.

Approach #2 : Using zip and unpacking(*) operator This method uses zip with * or unpacking operator which passes all the items inside the ‘lst’ as arguments to zip function. There is a little trick with extracting last item of list, Instead of using direct zip, use the reversed list iterators. 

Python3




# Python3 program to extract first and last
# element of each sublist in a list of lists
 
def Extract(lst):
    return list(zip(*[reversed(el) for el in lst]))[0]
     
# Driver code
lst = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
print(Extract(lst))


Output:

(3, 5, 9)

Time Complexity: O(N)
Auxiliary Space: O(1)

Another way of using zip is to use it with Python map, passing reversed as function. 

Python3




def Extract(lst):
    return list(list(zip(*map(reversed, lst)))[0])


Time Complexity: O(NlogN)
Auxiliary Space: O(N)

Approach #3 : Using itemgetter() 

Python3




# Python3 program to extract first and last
# element of each sublist in a list of lists
from operator import itemgetter
 
def Extract(lst):
    return list( map(itemgetter(-1), lst ))
     
# Driver code
lst = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
print(Extract(lst))


Output:

[3, 5, 9]

Time Complexity: O(NlogN)
Auxiliary Space: O(N)

Approach #4 : Using len() method and indexing

Python3




# Python3 program to extract last
# element of each sublist in a list of lists
 
def Extract(lst):
    res=[]
    for i in lst:
        res.append(i[len(i)-1])
    return res
# Driver code
lst = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
print(Extract(lst))


Output

[3, 5, 9]

Time Complexity: O(N)
Auxiliary Space: O(N)

Approach #5: Using the map() function and lambda expression
You could use the map() function and indexing to extract the last element of each sublist in a single line of code. For example:

Python3




# Python3 program to extract last
# element of each sublist in a list of lists
def Extract(lst):
    return list(map(lambda x: x[-1], lst))
# Driver code
lst = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
print(Extract(lst))


Output

[3, 5, 9]

Time complexity: O(N),  where N is the total number of sublists in the list.
Auxiliary space: O(N), where N is the total number of sublists in the list.

RELATED ARTICLES

Most Popular

Recent Comments