Check if numpy array is in list of numpy arrays

numpy, python

Solution

There is a much simpler way without the need to loop using np.all(). It only works when all the arrays within the list of arrays have the same shape:

list_np_arrays = np.array([[1., 1.], [1., 2.]])
array_to_check = np.array([1., 2.])

is_in_list = np.any(np.all(array_to_check == list_np_arrays, axis=1))

The variable is_in_list indicates if there is any array within he list of numpy arrays which is equal to the array to check.

Problem

I have a list of numpy arrays and a single numpy array. I want to check if that single array is a member of the list. I suppose there exist a method and I haven't searched properly... This is what I came up with: ``` def inList(array, list): for element in list: if np.array_equal(element, array): return True return False ``` Is this implementation correct? Is there any ready function for this?

Original source

Related problems