Le't assume I have a list with elements, from which I want to remove a few rows on the basis of a list of row indexes. This is an example:
l = ['ciao','hopefully','we','are','going','to','sort','this','out']
idx = [1,3,5]
If I do the following, it doesn't work as the loop doesn't consider that the lenght of the list after the nth remove object is n-i:
for x in idx:
del l[x]
# what happens? The code only removes correctly the first element of idx, then it doesn't take into account that the list has shrunk and therefore the nth index no longer corresponds to the updated row list.
Note, I cannot turn the list into an array and then use np.delete
as my list is coming from webscraping and it fails when I try to do so.
Can anyone suggest me a way to remove list's elements on the basis of its rows in one shot without messing up with the shifting of the index?
Thanks!