Python | Getting sublist 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 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 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)) |
The original list : [['Geeks', 1, 15], ['for', 3, 5], ['Geeks', 3, 7]] The first element of sublist till N : ['Geeks', 'for']
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 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)) |
The original list : [['Geeks', 1, 15], ['for', 3, 5], ['Geeks', 3, 7]] The first element of sublist till N : ['Geeks', 'for']
Recommended Posts:
- Python | Get last element of each sublist
- Python | Get first element of each sublist
- Python | Multiply each element in a sublist by its index
- Python | Sort a list according to the second element in sublist
- Python | Get elements till particular element in list
- Python | Maximum Sum Sublist
- Python | Indexing a sublist
- Python | Find maximum value in each sublist
- Python | Shift sublist in list
- Python | Replace sublist with other in list
- Python | Split Sublist Strings
- Python | Remove sublists that are present in another sublist
- Python | Find the sublist with maximum value in given nested list
- Python | Repeat String till K
- Python | Prefix Sum Subarray till False value
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



