Python | Get first element of each sublist
Last Updated :
21 Mar, 2023
Given a list of lists, write a Python program to extract first element of each sublist in the given list of lists. Examples:
Input : [[1, 2], [3, 4, 5], [6, 7, 8, 9]]
Output : [1, 3, 6]
Input : [['x', 'y', 'z'], ['m'], ['a', 'b'], ['u', 'v']]
Output : ['x', 'm', 'a', 'u']
Approach #1 : List comprehension
Python3
def Extract(lst):
return [item[ 0 ] for item in lst]
lst = [[ 1 , 2 ], [ 3 , 4 , 5 ], [ 6 , 7 , 8 , 9 ]]
print (Extract(lst))
|
Time Complexity: O(n)
Space Complexity: O(n) (length of the list given as argument)
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. Thus, all the first element will become the first tuple of the zipped list. Returning the 0th element will thus, solve the purpose.
Python3
def Extract(lst):
return list ( list ( zip ( * lst))[ 0 ])
lst = [[ 1 , 2 ], [ 3 , 4 , 5 ], [ 6 , 7 , 8 , 9 ]]
print (Extract(lst))
|
Time Complexity: O(n) (zip function has complexity of O(1) and conversion to list has O(n))
Space Complexity: O(n) (length of the list given as argument)
Another method of using zip is given below:-
Python3
def Extract(lst):
return list ( next ( zip ( * lst)))
|
Approach #3 : Using itemgetter()
Python3
from operator import itemgetter
def Extract(lst):
return list ( map (itemgetter( 0 ), lst ))
lst = [[ 1 , 2 ], [ 3 , 4 , 5 ], [ 6 , 7 , 8 , 9 ]]
print (Extract(lst))
|
Time Complexity: O(n) (itemgetter has O(1) and conversion to list O(n))
Space Complexity: O(n)
Approach #4 : Using reduce
You can use the reduce function from the functools library to iteratively apply a lambda function to the list of lists and build a new list containing the first elements of each sublist.
Python3
from functools import reduce
def extract_first(lst):
return reduce ( lambda acc, x: acc + [x[ 0 ]] if x else acc, lst, [])
lst = [[ 1 , 2 ], [ 3 , 4 , 5 ], [ 6 , 7 , 8 , 9 ]]
print (extract_first(lst))
|
Time Complexity: O(n)
Space Complexity: O(n)
Method#5: using for loop
Approach
this approach uses a for loop to iterate through each sublist and append the first element of each sublist to a new list. The resulting list is returned.
Algorithm
1. Initialize an empty list to hold the first elements.
2. Iterate through each sublist.
3. For each sublist, append the first element to the list of first elements.
4. Return the list of first elements.
—
Python3
def get_first_element(list_of_lists):
first_elements = []
for sublist in list_of_lists:
first_elements.append(sublist[ 0 ])
return first_elements
list_of_lists = [[ 1 , 2 ], [ 3 , 4 , 5 ], [ 6 , 7 , 8 , 9 ]]
print (get_first_element(list_of_lists))
|
Time complexity: O(n), where n is the number of sublists in the input list. We iterate once over each sublist.
Space complexity: O(n), where n is the number of sublists in the input list. We create a new list to hold the first elements.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...