Open In App

Python | Getting sublist element till N

Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, we may come across a utility in which we require to get the first N sublist elements that too only a particular index. This can have an application in queuing to get only the initial N person’s name. Let’s discuss certain ways in which this can be done. 

Method #1: Using list comprehension and list slicing The above two powerful Python utilities can be useful here as well to get the result as list comprehension can extract the element slicing can restrict the size we need to extract. 

Python3




# Python3 code to demonstrate
# getting sublist element till N
# using list comprehension + list slicing
 
# initializing list
test_list = [['Geeks', 1, 15], ['for', 3, 5], ['Geeks', 3, 7]]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing N
N = 2
 
# using list comprehension + list slicing
# getting sublist element till N
res = [i[0] for i in test_list[ : N]]
 
# print result
print("The first element of sublist till N : " + str(res))


Output : 

The original list : [['Geeks', 1, 15], ['for', 3, 5], ['Geeks', 3, 7]]
The first element of sublist till N : ['Geeks', 'for']

Time complexity: O(N), where N is the value of the variable N.
Auxiliary space: O(N), as the list comprehension creates a new list containing N elements.

Method #2: Using map() + itemgetter() + islice() The combination of above 3 functions can be used to perform this particular task. The itemgetter function gets the element to extract, islice slices till N and map function combines the result. 

Python3




# Python3 code to demonstrate
# getting sublist element till N
# using map() + itemgetter() + islice()
from operator import itemgetter
from itertools import islice
 
# initializing list
test_list = [['Geeks', 1, 15], ['for', 3, 5], ['Geeks', 3, 7]]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing N
N = 2
 
# using map() + itemgetter() + islice()
# getting sublist element till N
res = list(map(itemgetter(0), islice(test_list, 0, N)))
 
# print result
print("The first element of sublist till N : " + str(res))


Output : 

The original list : [['Geeks', 1, 15], ['for', 3, 5], ['Geeks', 3, 7]]
The first element of sublist till N : ['Geeks', 'for']

Time complexity : O(n), where n is length of test_list
Auxiliary space : O(n), where n is length of res list.

Method 3: Use a simple for loop

Python3




test_list = [['Geeks', 1, 15], ['for', 3, 5], ['Geeks', 3, 7]]
N = 2
 
res = []
for sublist in test_list[:N]:
    res.append(sublist[0])
 
print("The first element of sublist till N : " + str(res))


Output

The first element of sublist till N : ['Geeks', 'for']

Time complexity: O(N), where N is the number of sublists to extract from the original list. 
Auxiliary space: O(N), as we need to store the extracted elements in the res list.

Method #4: Using itertools module’s islice() function

Step-by-step approach

  • First, we import the itertools module which provides a collection of tools for handling iterators.
  • We initialize the test_list variable to a list of sublists, where each sublist contains three elements – a string, an integer, and another integer.
  • We print the original list using the print() function and concatenate it with a string.
  • We initialize the N variable to an integer value 2, which will be used to slice the list.
  • We use the itertools.islice() function to get a slice of the list up to the first N elements.
  • The itertools.islice() function takes two arguments – an iterable object and a slice object that specifies the start and stop positions of the slice.
  • Here, we use a generator expression (i[0] for i in test_list) as the iterable object, which generates the first element of each sublist in the test_list variable.
  • The list() function is used to convert the itertools.islice() object into a list.
  • Finally, we print the result using the print() function and concatenate it with a string.

Python3




import itertools
 
# Python3 code to demonstrate
# getting sublist element till N
# using itertools.islice() function
 
# initializing list
test_list = [['Geeks', 1, 15], ['for', 3, 5], ['Geeks', 3, 7]]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing N
N = 2
 
# using itertools.islice() function
# getting sublist element till N
res = list(itertools.islice((i[0] for i in test_list), N))
 
# print result
print("The first element of sublist till N : " + str(res))


Output

The original list : [['Geeks', 1, 15], ['for', 3, 5], ['Geeks', 3, 7]]
The first element of sublist till N : ['Geeks', 'for']

Time complexity: O(N) (where N is the number of elements in the sublist)
Auxiliary space: O(1)



Last Updated : 08 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads