Python | Slicing list from Kth element to last element
Python list slicing slices the list from start index till end – 1, specified as list elements. So its tricky when we require to also slice the last element of list. Trying to slice till list size + 1 gives an error. Let’s discuss ways in which last element can be included during a list slice.
Method #1 : Using None During list slicing, giving the desired first index K and specifying ‘None’ as the second argument in slicing works internally as slicing all the elements from K in list till end including it.
Python3
# Python3 code to demonstrate # list slicing from K to end # using None # initializing list test_list = [ 5 , 6 , 2 , 3 , 9 ] # printing original list print ("The original list is : " + str (test_list)) # index to begin slicing K = 2 # using None # to perform list slicing from K to end res = test_list[K : None ] # printing result print ("The sliced list is : " + str (res)) |
The original list is : [5, 6, 2, 3, 9] The sliced list is : [2, 3, 9]
Method #2 : Leaving the last element empty Usually, not specifying any element as end element of slicing instructs python to include whole list after K in list. But the main drawback in using this is code readability. Hence above method is preferred more than this.
Python3
# Python3 code to demonstrate # list slicing from K to end # without specifying last element # initializing list test_list = [ 5 , 6 , 2 , 3 , 9 ] # printing original list print ("The original list is : " + str (test_list)) # index to begin slicing K = 2 # without specifying last element # to perform list slicing from K to end res = test_list[K :] # printing result print ("The sliced list is : " + str (res)) |
The original list is : [5, 6, 2, 3, 9] The sliced list is : [2, 3, 9]
Method #3 : Using itertools
Here is an example of using the islice() function from the itertools module to slice a list from the Kth element to the last element:
Python3
from itertools import islice # Initialize the list test_list = [ 5 , 6 , 2 , 3 , 9 ] # Index to begin slicing K = 2 # Use islice() to slice the list from the Kth element to the last element res = list (islice(test_list, K, None )) print (res) # [2, 3, 9] #This code is contributed by Edula Vinay Kumar Reddy |
[2, 3, 9]
Time complexity: O(n)
Auxiliary space: O(n)
Please Login to comment...