Python | Delete elements in range
Last Updated :
17 May, 2023
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
test_list = [ 3 , 5 , 6 , 7 , 2 , 10 ]
indices_list = [ 1 , 4 , 2 ]
print ("The original list is : " + str (test_list))
print ("The indices list is : " + str (indices_list))
for i in sorted (indices_list, reverse = True ):
del test_list[i]
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
test_list = [ 3 , 5 , 6 , 7 , 2 , 10 ]
indices_list = [ 1 , 4 , 2 ]
print ("The original list is : " + str (test_list))
print ("The indices list is : " + str (indices_list))
test_list[:] = [ j for i, j in enumerate (test_list)
if i not in indices_list ]
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
indices_list = sorted (indices_list, reverse = True )
del test_list[indices_list[ 0 ]]
return delete_elements_recursive(test_list, indices_list[ 1 :])
test_list = [ 3 , 5 , 6 , 7 , 2 , 10 ]
indices_list = [ 1 , 4 , 2 ]
print ( "The original list is: " + str (test_list))
print ( "The indices list is: " + str (indices_list))
modified_list = delete_elements_recursive(test_list, indices_list)
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:
- Initialize the original list and indices list.
- Print the original list and indices list.
- Use the filter() function to filter out the elements of the original list that correspond to the indices in the indices list.
- Assign the filtered list to the original list variable.
- Print the modified list.
Python3
test_list = [ 3 , 5 , 6 , 7 , 2 , 10 ]
indices_list = [ 1 , 4 , 2 ]
print ( "The original list is : " + str (test_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))
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:
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
Share your thoughts in the comments
Please Login to comment...