Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to check whether specified values are present in NumPy array?
NumPy provides several methods to check whether specified values are present in an array. The most common approaches are using the in keyword, np.isin() function, and np.where() function.
Using the "in" Keyword
The in keyword checks if a single element exists in the array ?
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 4 is present in the array.")
else:
print("The element 4 is not present in the array.")
The Original array: [ 10. 30. 2. 40.3 56. 456. 32. 4. ] The element 4 is present in the array.
Checking Multiple Values
To check multiple values, use separate in statements ?
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 in arr and 56 in arr:
print("Both elements 4 and 56 are present in the array.")
else:
print("One or both elements are not present in the array.")
The Original array: [10. 30. 2. 40.3 56. 4. 56. 3. 2. 4. ] Both elements 4 and 56 are present in the array.
Using np.isin() Function
The np.isin() function returns a boolean array showing which elements match the specified values ?
import numpy as np
arr = np.array([[10, 30, 2, 40.3], [56, 4, 56, 3]])
print("The Original array:")
print(arr)
values = np.array([10, 30, 2, 40.3])
result = np.isin(arr, values)
print("\nBoolean result:")
print(result)
print("\nAny matches found:", np.any(result))
The Original array: [[10. 30. 2. 40.3] [56. 4. 56. 3. ]] Boolean result: [[ True True True True] [False False False False]] Any matches found: True
3D Array Example
import numpy as np
arr = np.array([[[10, 30], [2, 40.3]], [[56, 4], [56, 3]]])
print("The Original array:")
print(arr)
values = np.array([1, 40.3])
result = np.isin(arr, values)
print("\nBoolean result:")
print(result)
The Original array: [[[10. 30. ] [ 2. 40.3]] [[56. 4. ] [56. 3. ]]] Boolean result: [[[False False] [False True]] [[False False] [False False]]]
Using np.where() Function
The np.where() function returns the indices where the condition is true ?
import numpy as np
arr = np.array([[[10, 30], [2, 40.3]], [[56, 4], [56, 3]]])
print("The Original array:")
print(arr)
indices = np.where(arr == 3)
print("\nIndices where value is 3:")
print(indices)
if len(indices[0]) > 0:
print("Value 3 found at positions:", list(zip(*indices)))
else:
print("Value 3 not found in the array")
The Original array: [[[10. 30. ] [ 2. 40.3]] [[56. 4. ] [56. 3. ]]] Indices where value is 3: (array([1]), array([1]), array([1])) Value 3 found at positions: [(1, 1, 1)]
Comparison
| Method | Best For | Returns |
|---|---|---|
in keyword |
Single value check | Boolean (True/False) |
np.isin() |
Multiple values, element-wise check | Boolean array |
np.where() |
Finding positions of values | Tuple of indices |
Conclusion
Use in for simple single value checks, np.isin() for multiple values or element-wise comparisons, and np.where() when you need to find the exact positions of matching elements.
Advertisements
