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
test_list = [5, 6, 2, 3, 9]
print ("The original list is : " + str(test_list))
K = 2
res = test_list[K : None]
print ("The sliced list is : " + str(res))
|
Output:
The original list is : [5, 6, 2, 3, 9]
The sliced list is : [2, 3, 9]
Time complexity: O(n), where n is the length of test_list.
Auxiliary space: O(n – K), as new list is created to store the sliced sublist.
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
test_list = [5, 6, 2, 3, 9]
print ("The original list is : " + str(test_list))
K = 2
res = test_list[K :]
print ("The sliced list is : " + str(res))
|
Output:
The original list is : [5, 6, 2, 3, 9]
The sliced list is : [2, 3, 9]
Time complexity: O(n), where n is the length of the test_list.
Auxiliary space: O(n – K), as we are creating a new list to store the sliced sublist.
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
test_list = [5, 6, 2, 3, 9]
K = 2
res = list(islice(test_list, K, None))
print(res)
|
Time complexity: O(n)
Auxiliary space: O(n)
Method 4: Using a for loop to iterate over the list and append elements to a new list starting from the K index.
Step-by-step approach:
- Initialize an empty list to store the sliced list.
- Initialize a variable ‘i’ to 0.
- Iterate over the original list from index ‘K’ to the end using a for loop.
- Append each element to the empty list initialized in step 1.
- Increment ‘i’ by 1 after each iteration.
- Print the sliced list.
Below is the implementation of the above approach:
Python3
test_list = [5, 6, 2, 3, 9]
print ("The original list is : " + str(test_list))
K = 2
res = []
for i in range(K, len(test_list)):
res.append(test_list[i])
print ("The sliced list is : " + str(res))
|
Output
The original list is : [5, 6, 2, 3, 9]
The sliced list is : [2, 3, 9]
Time complexity: O(n), where ‘n’ is the length of the original list.
Auxiliary space: O(n), as we are creating a new list to store the sliced elements.
Method #5: Using list comprehension
- Initialize the input list and the index K
- Use list comprehension to slice the input list from index K to the end.
- Assign the sliced list to a variable ‘res’.
- Print the sliced list.
Python3
test_list = [5, 6, 2, 3, 9]
print ("The original list is : " + str(test_list))
K = 2
res = [test_list[i] for i in range(K, len(test_list))]
print ("The sliced list is : " + str(res))
|
Output
The original list is : [5, 6, 2, 3, 9]
The sliced list is : [2, 3, 9]
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), as we are creating a new list to store the sliced element
Method #6: Using numpy:
Algorithm:
- Initialize the list.
- Initialize the starting index to slice from.
- Create a numpy array from the list slicing from the starting index to the end.
- Convert the numpy array back to a list.
- Print the sliced list.
Python3
import numpy as np
test_list = [5, 6, 2, 3, 9]
print("The original list is:", test_list)
K = 2
res = np.array(test_list[K:])
print("The sliced list is:", res.tolist())
|
Output:
The original list is: [5, 6, 2, 3, 9]
The sliced list is: [2, 3, 9]
Time Complexity: The time complexity of the code is O(n), where n is the number of elements in the list. This is because we are creating a numpy array from the list which takes O(n) time, and then converting the numpy array back to a list which also takes O(n) time. All other operations are constant time.
Space Complexity: The space complexity of the code is O(n), where n is the number of elements in the list. This is because we are creating a numpy array from the list which takes O(n) space, and then converting the numpy array back to a list which also takes O(n) space. All other operations are constant space.
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
18 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...