Search code examples
pythonperformancenumpy

What is the most efficient way to check if a value exists in a NumPy array?


I have a very large NumPy array

1 40 3
4 50 4
5 60 7
5 49 6
6 70 8
8 80 9
8 72 1
9 90 7
.... 

I want to check to see if a value exists in the 1st column of the array. I've got a bunch of homegrown ways (e.g. iterating through each row and checking), but given the size of the array I'd like to find the most efficient method.

Thanks!


Solution

  • How about

    if value in my_array[:, col_num]:
        do_whatever
    

    Edit: I think __contains__ is implemented in such a way that this is the same as @detly's version