user7725010
user7725010

Reputation: 102

python3 delete more than one elements in a list using another list as index

import numpy as np
m = np.arange(10).tolist()
n = [2, 6, 4]

I want to delete the 2nd, 6th, and 4th elements in list m.

del m[n] 

Traceback (most recent call last): File "", line 1, in TypeError: list indices must be integers or slices, not list

I tried this:

ns = np.sort(n)

for i in np.arange(len(ns)):
    m.pop(ns[i] - i)

but it pop out the deleted elements Is there any elegant method to do this job?

Upvotes: 0

Views: 46

Answers (1)

Louis T.
Louis T.

Reputation: 81

For this simple case, you can use m = np.delete(m, n).

Here is a link to the doc : https://docs.scipy.org/doc/numpy/reference/generated/numpy.delete.html

Upvotes: 1

Related Questions