Skip to content
Advertisement

How to remove multiple object from the list at the same time

JavaScript

I am trying to write a code such that it removes adjacent (+n, -n) pairs such as (3,-3) (4,-4) etc.

However I cannot use del[i] since when I remove an element the xarray also changes. I tried to use deepcopy to copy the x array but I couldnt manage to run the code.

JavaScript

Advertisement

Answer

You can sort the list of indices in descending order so that deleting an element will not effect the position of another. You can do this by reversed(ind)

JavaScript

Or if your indices are not in any order you can always sort them in descending order.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement