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 and delete them in the original list in backward manner so that the rearrangement of list doesn’t destroy the integrity of the solution.
# 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)) |
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 #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 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)) |
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]
Recommended Posts:
- Python | Delete elements with frequency atmost K
- numpy.delete() in Python
- MongoDB python | Delete Data and Drop Collection
- Python | Pandas Index.delete()
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- Python | Delete items from dictionary while iterating
- Python | Pandas TimedeltaIndex.delete
- Python Program to delete a file
- Delete a directory or file using Python
- Delete an entire directory tree using Python | shutil.rmtree() method
- How to delete data from file in Python
- Python MySQL - Delete Query
- DELETE method- Python requests
- Delete Google Browser History using Python
- Python del to delete objects
- How to Delete files in Python using send2trash module?
- Python - Test if List contains elements in Range
- Python Program for Number of elements with odd factors in given range
- Python | Print list elements in circular range
- Python | Find elements within range in numpy
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

