NumPy: Checking if an array contains a value (4 examples)

Updated: March 1, 2024 By: Guest Contributor Post a comment

Introduction

NumPy, an essential library in Python for scientific computing, offers various ways to interact with arrays. One common task is checking if an array contains a specific value. This tutorial will guide you through four examples, starting from basic to advanced techniques, to accomplish this task efficiently.

Example #1 – Basic Check with the in Operator

The simplest way to check if an array contains a specific value is by using the in operator. This method is straightforward but can be slow for large arrays as it implicitly performs a linear search.

import numpy as np

# Creating a sample array
array = np.array([1, 2, 3, 4, 5])

# Checking if 3 is in the array
print(3 in array)
# Output: True

Example #2 – Using numpy.where()

The numpy.where() function is a versatile tool for conditionally searching for values. It returns the indices where a specified condition is true. If you’re searching for the existence of a value, combine it with any() or all() to get a boolean answer.

import numpy as np

# Creating a sample array
array = np.array([1, 2, 3, 4, 5])

# Searching for 3 in the array
result = np.where(array == 3)

# Checking if the search was successful
print(any(result))
# Output: True

Example #3 – Checking for Value with np.isin()

The np.isin() function is designed specifically for testing membership. It checks each element in an array against a value (or values) and returns an array of booleans indicating the presence or absence of the target value.

import numpy as np

# Creating a sample array
array = np.array([1, 2, 3, 4, 5])

# Checking if the array contains 3
print(np.isin(3, array))
# Output: True

Example #4 – Performance-oriented Approach with numpy.argmax() and numpy.argmin()

For those who need a faster solution, particularly with very large arrays, combining numpy.argmax() or numpy.argmin() with logical operations can prove significantly more efficient. The idea is to use these functions to quickly identify if at least one true condition (the presence of the value) exists within the array. However, this method requires caution, as it will return 0 for both cases when the sought-after value is at position 0 and when it is not found (but other operations can detect the latter scenario).

import numpy as np

# Creating a large array for demonstration
large_array = np.random.randint(1, 100, size=10000)

# Checking if 57 is in the large array
found = (large_array == 57).any()
print(found)
# Output: Depends on the random contents of the array

Conclusion

Knowing how to efficiently check if a specific value exists in a NumPy array is a fundamental skill for anyone working with data in Python. Starting from the simple in operator to more advanced techniques like np.isin(), the choice of method depends on your specific needs and the size of the array. For most scenarios, np.isin() offers a straightforward, readable solution. However, for large-scale data, a performance-oriented approach might be preferable. Experiment with these tactics to find which best suits your project.