Open In App

numpy.isin() method - Python

Last Updated : 27 Sep, 2025
Suggest changes
Share
1 Likes
Like
Report

The numpy.isin() method provides a way to check whether elements of one NumPy array exist in another array and returns a boolean array indicating which elements are present.

An example to see if a single element exists in a NumPy array.

Python
import numpy as np
arr = np.array([5, 10, 15, 20])
res = np.isin(10, arr)  
print(res)

Output
True

Explanation: The element 10 is present in the array [5, 10, 15, 20], so the output is True.

Syntax

numpy.isin(element, test_elements, assume_unique=False, invert=False)

Parameters:

  • element: Input array whose values are checked.
  • test_elements: values to check against (can be a list, array, or set).
  • assume_unique: If True, function assumes both arrays contain unique elements (for faster computation). Default is False.
  • invert: If True, returns True for elements not in test_elements. Default is False.

Return Value: A boolean array of same shape as element, indicating True where element is in test_elements.

Examples

Example 1: Here we check which elements of a 1-D array exist in a given list.

Python
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
li = [1, 3, 5]
res = np.isin(arr, li)
print(res)

Output
[ True False  True False  True]

Explanation:

  • Element 1 -> present -> True
  • Element 2 -> not present -> False
  • Element 3 -> present -> True
  • Element 4 -> not present -> False
  • Element 5 -> present -> True

Example 2: This example checks which elements of a 2-D array are present in a list of values.

Python
import numpy as np
arr = np.array([[1, 3], [5, 7], [9, 11]])
li = [1, 3, 11, 9]
res = np.isin(arr, li)
print(res)

Output
[[ True  True]
 [False False]
 [ True  True]]

Explanation:

  • Row 0 -> [1, 3] -> both in list -> [True, True]
  • Row 1 -> [5, 7] -> neither in list -> [False, False]
  • Row 2 -> [9, 11] -> both in list -> [True, True]

Example 3: We can also find elements not present in the reference list using invert=True.

Python
import numpy as np
arr = np.array([10, 20, 30, 40])
li = [20, 40, 50]
res = np.isin(arr, li, invert=True)
print(res)

Output
[ True False  True False]

Explanation:

  • Element 10 -> not in li -> True
  • Element 20 -> in li -> False
  • Element 30 -> not in li -> True
  • Element 40 -> in li -> False

Article Tags :

Explore