Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete an element of certain value in numpy array once

I would like to delete an element from a numpy array that has a certain value. However, in the case there are multiple elements of the same value, I only want to delete one occurrence (doesn't matter which one). That is:

import numpy as np
a = np.array([1, 1, 2, 6, 8, 8, 8, 9])

How do I delete one instance of 8? Specifically

a_new = np.delete(a, np.where(a == 8))
print(a_new)

removes all 8's.

like image 368
Sid Avatar asked Oct 24 '25 16:10

Sid


1 Answers

You can simply choose one of the indices:

In [3]: np.delete(a, np.where(a == 8)[0][0])
Out[3]: array([1, 1, 2, 6, 8, 8, 9])
like image 146
Mazdak Avatar answered Oct 27 '25 05:10

Mazdak