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

w = [1, 2, 3, -3, -2, 4, -4, 1, 2, -2]


def data_remover(xarray, ind):
    for i in ind:
        del xarray[i]
    return xarray


print(data_remover(w, [2, 3, 5, 6, 8, 9]))

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.

from copy import deepcopy

w = [1, 2, 3, -3, -2, 4, -4, 1, 2, -2]


def data_remover(xarray, ind):
    xcopy = deepcopy(xarray)
    for i in ind:
        del xarray[i]
    return xarray


print(data_remover(w, [2, 3]))


Read more here: https://stackoverflow.com/questions/65717323/how-to-remove-multiple-object-from-the-list-at-the-same-time

Content Attribution

This content was originally published by Arman Çam at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: