How to check whether specified values are present in NumPy array?


We have different modules and functions available in python language to check whether specified values are present in the given Numpy array.

Numpy is abbreviated as Numerical Python, which is a python library used to perform the mathematical, statistical and scientific calculations. The result of the numpy array is returned in the format of array. The arrays can be created in one dimension, two dimension and so on up to 32 dimensions.

The Numpy library provides a number of modules and functions which helps us to perform scientific calculations and mathematical calculations.

Let’s see each way one by one to check the specified values present in the numpy array.

Using the “in” keyword

Following example checks whether specified values are present in Numpy array. We have the keyword namely in, which is used to check if the particular element is present in the defined data structure.

import numpy as np
arr = np.array([10,30,2,40.3,56,456,32,4])
print("The Original array:",arr)
if 4 in arr:
    print("The element is present in the array.")
else:
    print("The element is not present in the array")

Output

When we run the above code, following output will be generated, it shows that the element is present in the array.

The Original array: [ 10.   30.    2.   40.3  56.  456.   32.    4. ]
The element is present in the array.

Example

This is another example to check whether the given value is present in the defined array.

import numpy as np
arr = np.array([10,30,2,40.3,56,4,56,3,2,4])
print("The Original array:",arr)
if 4 or 56 in arr:
    print("The element is present in the array.")
else:
    print("The element is not present in the array")

Output

The following is the output of the in keyword to check the whether the given value is present in the array.

The Original array: [10.  30.   2.  40.3 56.   4.  56.   3.   2.   4. ]
The element is present in the array.

Using the np.isin() function

Numpy library, provides a function namely, isin() which is used to check if the given value is present in the defined array. The values to be checked should also be in the array format. The output will be returned in the Boolean format of True or False.

Example

In this example, we will pass the array to be checked and values to the isin() function of the numpy library then the output will be returned in the format of Boolean values True or False.

import numpy as np
arr = np.array([[10,30,2,40.3],[56,4,56,3]])
print("The Original array:",arr)
values = np.array([10,30,2,40.3])
output = np.isin(arr, values)
print(output)

Output

The following is the output of the isin() function which returns the Boolean values.

The Original array: [[10.  30.   2.  40.3]
 [56.   4.  56.   3. ]]
[[ True  True  True  True]
 [False False False False]]

Example

Following example shows how to check whether the specified values are present in the defined array.

import numpy as np
arr = np.array([[[10,30],[2,40.3]],[[56,4],[56,3]]])
print("The Original array:",arr)
values = np.array([1,40.3])
output = np.isin(arr, values)
print(output)

Output

The below is the output of the above mentioned code.

The Original array: [[[10.  30. ]
  [ 2.  40.3]]

 [[56.   4. ]
  [56.   3. ]]]
[[[False False]
  [False  True]]

 [[False False]
  [False False]]]

Using np.where() function

where() is a function provided by numpy library that allows you to search for a specific value within a numpy array. This function returns the indices of elements in array where the value is present.

Example

To check whether a specific value is present in the NumPy array, we use the where() function provided by NumPy array. To this function we will pass the array and the value, to be searched as arguments.

import numpy as np
arr = np.array([[[10,30],[2,40.3]],[[56,4],[56,3]]])
print("The Original array:",arr)
output = np.where(arr == 3)
print(output)

Output

When we run the above code, we see the following output -

The Original array: [[[10.  30. ]
  [ 2.  40.3]]

 [[56.   4. ]
  [56.   3. ]]]
(array([1]), array([1]), array([1]))

Updated on: 09-Aug-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements