Python | Delete elements in range
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
- Python | Assign range of elements to List
- Python | Find elements within range in numpy
- Python | Print list elements in circular range
- Python Program for Number of elements with odd factors in given range
- numpy.delete() in Python
- Python Program to delete a file
- Python | Pandas Index.delete()
- Python | Pandas TimedeltaIndex.delete
- Delete a directory or file using Python
- Python | Delete items from dictionary while iterating
- MongoDB python | Delete Data and Drop Collection
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- Delete an entire directory tree using Python | shutil.rmtree() method
- Python | range() method
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.