Open In App

Python | Front and rear range deletion in a list

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# front and rear deletion
# using del operator + list slicing
 
# initializing list
test_list = [2, 3, 5, 7, 9, 10, 8, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using del operator + list slicing
# front and rear deletion
del test_list[-2:], test_list[:2]
 
# printing result
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




# Python3 code to demonstrate
# front and rear deletion
# using list slicing
 
# initializing list
test_list = [2, 3, 5, 7, 9, 10, 8, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# using list slicing
# front and rear deletion
res = test_list[2 : -2]
 
# printing result
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




# Initialize the list
test_list = [2, 3, 5, 7, 9, 10, 8, 6]
 
# Print the original list
print("The original list is:", test_list)
 
# Delete the first two and last two elements
cropped_list = test_list[slice(2, -2)]
 
 
# Print the cropped list
print("The cropped list is:", cropped_list)
 
# Time complexity: O(n)
# Space complexity: O(1)


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), 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 :

  1. Initialize a list named “test_list” with some integer values.
  2. Print the original list.
  3. 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.
  4. 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.
  5. Assign the resulting list to a new variable named “cropped_list”.
  6. Print the resulting list.

Python3




# Python3 code to demonstrate
# front and rear deletion
# using del
 
# initializing list
test_list = [2, 3, 5, 7, 9, 10, 8, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
 
del test_list[0:2]
del test_list[-2:]
cropped_list = test_list
 
# printing result
print ("The cropped list is : " + str(cropped_list))
#this code contributed by tvsk


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) 
Auxiliary space: O(1), where n is the number of elements in the list.

Method #5: Using list comprehension

Python3




# initializing list
test_list = [2, 3, 5, 7, 9, 10, 8, 6]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using list comprehension
# front and rear deletion
test_list = [test_list[i] for i in range(2, len(test_list)-2)]
 
# printing result
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]

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:

  1. Initialize a variable i to 0
  2. Pop the first element from the list using pop() method and increment i
  3. Repeat step 2 until i reaches 2
  4. Initialize a variable j to 0
  5. Pop the last element from the list using pop() method and increment j
  6. Repeat step 5 until j reaches 2
  7. Print the cropped list

Below is the implementation of the above approach:

Python3




# initializing list
test_list = [2, 3, 5, 7, 9, 10, 8, 6]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using pop() method and a loop
# front and rear deletion
i = 0
while i < 2:
    test_list.pop(0)
    i += 1
 
j = 0
while j < 2:
    test_list.pop()
    j += 1
 
# printing result
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]

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:

  1. Create a list test_list with 8 elements.
  2. Print the original list using the print() function.
  3. Convert test_list to a numpy array using np.array().
  4. Use numpy slicing to select the elements from index 2 to index -2 (excluding the last two elements) and store it in res.
  5. Convert res back to a list using the tolist() method.
  6. Print the cropped list using the print() function.

Python3




import numpy as np
 
# initializing list
test_list = [2, 3, 5, 7, 9, 10, 8, 6]
 
# printing original list
print("The original list is:", test_list)
 
# using numpy front and rear deletion
arr = np.array(test_list)
res = arr[2:-2]
 
# Print the result
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.



Last Updated : 22 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads