We often come to the situations in which we need to extract initial K elements of list. This particular problem occurs when we need to optimize memory. This has its application in the day-day programming when sometimes we require to get all the lists of similar size. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using len() + list slicing List slicing can perform this particular task in which we just slice the first K elements to be in the list and hence removing the remaining elements, freeing memory.
Python3
# Python code to demonstrate # K elements Slicing # using len() + list slicing # initializing list test_list = [ 1 , 4 , 6 , 3 , 5 , 8 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 4 # using len() + list slicing # K elements Slicing res = test_list[: K] # printing result print ( "The K sliced List : " + str (res)) |
The original list is : [1, 4, 6, 3, 5, 8] The K sliced List : [1, 4, 6, 3]
Time Complexity: O(n),The above code iterates through the list once, hence the time complexity is linear, i.e. O(n).
Auxiliary Space: O(n),The algorithm uses an additional list to store the result, thus consuming linear space which is O(n).
Method #2 : Using Negative list slicing We can perform this particular task using the negative list slicing in which we start removing the elements from the last index of the list and hence remove all the N – K elements from the last. We remove None if 0 elements are asked to be removed.
Python3
# Python code to demonstrate # K elements Slicing # using negative list slicing # initializing list test_list = [ 1 , 4 , 6 , 3 , 5 , 8 ] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 4 # using negative list slicing # K elements Slicing res = test_list[ : - ( len (test_list) - K)] # printing result print ( "The K sliced List : " + str (res)) |
The original list is : [1, 4, 6, 3, 5, 8] The K sliced List : [1, 4, 6, 3]
Time Complexity: O(n) where n is the number of elements in the string list. The Negative list slicing is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res test_list.
Method #3 : Using itertools
Another approach to slice a list to get the first K elements is by using the itertools.islice() function.
Python3
import itertools # Python program to slice a list to get the first K elements test_list = [ 1 , 4 , 6 , 3 , 5 , 8 ] # using itertools.islice() function K = 4 res = list (itertools.islice(test_list, K)) # printing result print ( "The K sliced List :" , res) #This code is contributed by Edula Vinay Kumar Reddy |
The K sliced List : [1, 4, 6, 3]
Time complexity: O(n) as it is iterating through the list once.
Auxiliary Space: O(k) as it is creating a new list with the first k elements of the original list
Method #4 : Using operator.getitem() , slice() methods
Approach
Get first K elements of list using operator.getitem(),slice() methods
Python3
# Python program to slice a list to get the first K elements import operator test_list = [ 1 , 4 , 6 , 3 , 5 , 8 ] # Using operator.getitem() , slice() methods K = 4 res = operator.getitem(test_list, slice ( 0 , K)) # printing result print ( "The K sliced List :" , res) |
The K sliced List : [1, 4, 6, 3]
Time complexity: O(n) as it is iterating through the list once.
Auxiliary Space: O(k) as it is creating a new list with the first k elements of the original list