Saturday, February 7, 2026
HomeLanguagesPython | Get Kth element till N

Python | Get Kth element till N

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 Kth 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
# Get Kth 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
 
# initializing K
K = 1
 
# using list comprehension + list slicing
# Get Kth element till N
res = [i[K] for i in test_list[ : N]]
 
# print result
print("The Kth element of sublist till N : " + str(res))


Output : 

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

Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

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
# Get Kth 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
 
# initializing K
K = 1
 
# using map() + itemgetter() + islice()
# Get Kth element till N
res = list(map(itemgetter(K), islice(test_list, 0, N)))
 
# print result
print("The Kth element of sublist till N : " + str(res))


Output : 

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

Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.

Method 3: Using a simple for loop

Use simple for loop to iterate over the list and extract the Kth element of each sublist till N. 

Python3




# 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
 
# initializing K
K = 1
 
# using for loop
# Get Kth element till N
res = []
for i in range(N):
    res.append(test_list[i][K])
 
# print result
print("The Kth element of sublist till N : " + str(res))


Output

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

Time complexity: O(N)
Auxiliary space: O(N), as we need to store the Kth element of each sublist till N in a separate list.

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32491 POSTS0 COMMENTS
Milvus
126 POSTS0 COMMENTS
Nango Kala
6862 POSTS0 COMMENTS
Nicole Veronica
11987 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12076 POSTS0 COMMENTS
Shaida Kate Naidoo
6996 POSTS0 COMMENTS
Ted Musemwa
7237 POSTS0 COMMENTS
Thapelo Manthata
6947 POSTS0 COMMENTS
Umr Jansen
6933 POSTS0 COMMENTS