Open In App

Python | Delete elements in range

Last Updated : 17 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The deletion of a single element comparatively easier, but when we wish to delete element in range, the task becomes a tedious once due to the rearrangements and shifting of list elements automatically in python. Let’s discuss certain ways in which elements can be deleted in range. 
Method #1 : Using del + sorted() In this method, we reverse the list of indices we wish to delete them in the original list in backward manner so that the rearrangement of list doesn’t destroy the integrity of the solution. 
 

Python3




# Python3 code to demonstrate
# range deletion of elements
# using del + sorted()
 
# initializing list
test_list = [3, 5, 6, 7, 2, 10]
 
# initializing indices
indices_list = [1, 4, 2]
 
# printing the original list
print ("The original list is : " + str(test_list))
 
# printing the indices list
print ("The indices list is : " + str(indices_list))
 
# using del + sorted()
# range deletion of elements
for i in sorted(indices_list, reverse = True):
    del test_list[i]
 
# printing result
print ("The modified deleted list is : " + str(test_list))


Output:

The original list is : [3, 5, 6, 7, 2, 10]
The indices list is : [1, 4, 2]
The modified deleted list is : [3, 7, 10]

Time Complexity: O(n*logn), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

  Method #2 : Using enumerate() + list comprehension This task can also be performed if we make a list that would not have the elements in the delete list, i.e rather than actually deleting the elements, we can remake it without them. 
 

Python3




# Python3 code to demonstrate
# range deletion of elements
# using enumerate() + list comprehension
 
# initializing list
test_list = [3, 5, 6, 7, 2, 10]
 
# initializing indices
indices_list = [1, 4, 2]
 
# printing the original list
print ("The original list is : " + str(test_list))
 
# printing the indices list
print ("The indices list is : " + str(indices_list))
 
# using enumerate() + list comprehension
# range deletion of elements
test_list[:] = [ j for i, j in enumerate(test_list)
                         if i not in indices_list ]
 
# printing result
print ("The modified deleted list is : " + str(test_list))


Output:

The original list is : [3, 5, 6, 7, 2, 10]
The indices list is : [1, 4, 2]
The modified deleted list is : [3, 7, 10]

 Method #3: Using Recursion:

Python3




def delete_elements_recursive(test_list, indices_list):
    if not indices_list:
        return test_list
 
    # Sort the indices list in reverse order
    indices_list = sorted(indices_list, reverse=True)
 
    # Delete elements from the test_list using del
    del test_list[indices_list[0]]
 
    # Recursively call the function with the remaining indices
    return delete_elements_recursive(test_list, indices_list[1:])
 
# initializing list
test_list = [3, 5, 6, 7, 2, 10]
 
# initializing indices
indices_list = [1, 4, 2]
 
# printing the original list
print("The original list is: " + str(test_list))
 
# printing the indices list
print("The indices list is: " + str(indices_list))
 
# calling the recursive function to delete elements
modified_list = delete_elements_recursive(test_list, indices_list)
 
# printing result
print("The modified deleted list is: " + str(modified_list))


Output

The original list is: [3, 5, 6, 7, 2, 10]
The indices list is: [1, 4, 2]
The modified deleted list is: [3, 7, 10]

Time Complexity: The time complexity of the recursive method depends on the number of elements in the indices_list. The sorting step has a time complexity of O(k log k), where k is the length of indices_list. The del operation and the slicing operation both take constant time, so the dominant factor in the time complexity is the sorting step. Therefore, the overall time complexity can be considered as O(k log k).

Space Complexity: The space complexity of the recursive method is determined by the depth of the recursion, which is equal to the length of the indices_list. In the worst case, the indices_list can have k elements, so the space complexity can be considered as O(k).

 Method #4: Using filter() and index() method:

Algorithm:

  1. Initialize the original list and indices list.
  2. Print the original list and indices list.
  3. Use the filter() function to filter out the elements of the original list that correspond to the indices in the indices list.
  4. Assign the filtered list to the original list variable.
  5. Print the modified list.

Python3




test_list = [3, 5, 6, 7, 2, 10]
indices_list = [1, 4, 2]
# printing the original list
print ("The original list is : " + str(test_list))
  
# printing the indices list
print ("The indices list is : " + str(indices_list))
test_list = list(filter(lambda x: test_list.index(x) not in indices_list, test_list))
 
# printing result
print ("The modified deleted list is : " + str(test_list))
#This code is contributed by Jyothi Pinjala


Output

The original list is : [3, 5, 6, 7, 2, 10]
The indices list is : [1, 4, 2]
The modified deleted list is : [3, 7, 10]

Time Complexity:
The time complexity of this code is O(n^2) where n is the length of the original list, because the list.index() function has a time complexity of O(n) and it is called for each element in the list.

Auxiliary Space:
The space complexity of this code is O(n) where n is the length of the original list, because the filter function returns a new list that contains the filtered elements, and this list has the same length as the original list.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads