Sometimes, we require to shrink a list by deletion of its certain elements. One of the methods that is employed to perform this particular task is front and rear element deletion. It is a good utility whose solution can be useful to have. Let’s discuss certain ways in which this can be performed.
Method #1: Using list slicing + del operator The del operator can be clubbed with the slicing action to delete the front and rear elements from a list to obtain a cropped version of list.
Python3
test_list = [ 2 , 3 , 5 , 7 , 9 , 10 , 8 , 6 ]
print ("The original list is : " + str (test_list))
del test_list[ - 2 :], test_list[: 2 ]
print ("The cropped list is : " + str (test_list))
|
Output :
The original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is : [5, 7, 9, 10]
Method #2: Using list slicing Above method can be modified and the use of del operator can be omitted to achieve this particular task. We can slice a list in a way that specific number of elements are removed from the list.
Python3
test_list = [ 2 , 3 , 5 , 7 , 9 , 10 , 8 , 6 ]
print ("The original list is : " + str (test_list))
res = test_list[ 2 : - 2 ]
print ("The cropped list is : " + str (res))
|
Output :
The original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is : [5, 7, 9, 10]
Time complexity: O(n), where n is the length of the original list test_list.
Auxiliary space complexity: O(n), where n is the length of the original list test_list.
Method #3: Using inbuilt methods
To delete a range of elements from the front and rear of a list in Python, you can use the slice() function.
The slice() function creates a slice object that can be used to slice a sequence. You can pass it three arguments: start, stop, and step, which specify the starting and ending indices of the slice, and the step size, respectively.
Here’s an example of how you could use the slice() function to delete a range of elements from the front and rear of a list:
Python3
test_list = [ 2 , 3 , 5 , 7 , 9 , 10 , 8 , 6 ]
print ( "The original list is:" , test_list)
cropped_list = test_list[ slice ( 2 , - 2 )]
print ( "The cropped list is:" , cropped_list)
|
OutputThe original list is: [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is: [5, 7, 9, 10]
Time complexity: O(n), because it takes linear time to delete the elements from the list.
Auxiliary space: O(n) for storing cropped list.
Method #4: Using del statement.
step-by-step approach :
- Initialize a list named “test_list” with some integer values.
- Print the original list.
- Delete the first two elements of the list using the “del” keyword and list slicing. The slicing expression [0:2] selects the first two elements, which are then deleted from the list.
- Delete the last two elements of the list using the “del” keyword and negative list indexing. The indexing expression [-2:] selects the last two elements, which are then deleted from the list.
- Assign the resulting list to a new variable named “cropped_list”.
- Print the resulting list.
Python3
test_list = [ 2 , 3 , 5 , 7 , 9 , 10 , 8 , 6 ]
print ( "The original list is : " + str (test_list))
del test_list[ 0 : 2 ]
del test_list[ - 2 :]
cropped_list = test_list
print ( "The cropped list is : " + str (cropped_list))
|
OutputThe original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is : [5, 7, 9, 10]
Time complexity: O(n)
Auxiliary space: O(1), where n is the number of elements in the list.
Method #5: Using list comprehension
Python3
test_list = [ 2 , 3 , 5 , 7 , 9 , 10 , 8 , 6 ]
print ( "The original list is : " + str (test_list))
test_list = [test_list[i] for i in range ( 2 , len (test_list) - 2 )]
print ( "The cropped list is : " + str (test_list))
|
OutputThe original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is : [5, 7, 9, 10]
Time complexity: (O(n), where n is length of test_list
Auxiliary space: O(n)
Method #6: Using pop() method and a loop
Step-by-step approach:
- Initialize a variable i to 0
- Pop the first element from the list using pop() method and increment i
- Repeat step 2 until i reaches 2
- Initialize a variable j to 0
- Pop the last element from the list using pop() method and increment j
- Repeat step 5 until j reaches 2
- Print the cropped list
Below is the implementation of the above approach:
Python3
test_list = [ 2 , 3 , 5 , 7 , 9 , 10 , 8 , 6 ]
print ( "The original list is : " + str (test_list))
i = 0
while i < 2 :
test_list.pop( 0 )
i + = 1
j = 0
while j < 2 :
test_list.pop()
j + = 1
print ( "The cropped list is : " + str (test_list))
|
OutputThe original list is : [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is : [5, 7, 9, 10]
Time complexity: O(n), where n is the length of the list, because we need to traverse the list twice, once for removing the first two elements and once for removing the last two elements
Auxiliary space: O(1), because we are modifying the original list in-place without creating any additional data structures.
Method #7: Using heapq:
- Create a list test_list with 8 elements.
- Print the original list using the print() function.
- Convert test_list to a numpy array using np.array().
- Use numpy slicing to select the elements from index 2 to index -2 (excluding the last two elements) and store it in res.
- Convert res back to a list using the tolist() method.
- Print the cropped list using the print() function.
Python3
import numpy as np
test_list = [ 2 , 3 , 5 , 7 , 9 , 10 , 8 , 6 ]
print ( "The original list is:" , test_list)
arr = np.array(test_list)
res = arr[ 2 : - 2 ]
print ( "The cropped list is:" , res.tolist())
|
Output:
The original list is: [2, 3, 5, 7, 9, 10, 8, 6]
The cropped list is: [5, 7, 9, 10]
Time Complexity: O(n), where n is the number of elements in the input list test_list because numpy slicing takes O(n) time.
Space Complexity: O(n), because we create a numpy array with n elements and a new list with n-4 elements.