Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

NumPy Arrays Search

Searching Arrays

You can search an array for a specific value and obtain the indices of matches using the where() method.

Example

Locate the indices where the value is 4:

import numpy as np

arr = np.array([1234544])

x = np.where(arr == 4)

print(x)

The previous example will return a tuple: (array([3, 5, 6],), indicating that the value 4 is located at indices 3, 5, and 6.

Example

Locate the indices where the values are even:

import numpy as np

arr = np.array([12345678])

x = np.where(arr%2 == 0)

print(x)

Example

Locate the indices where the values are odd:

import numpy as np

arr = np.array([12345678])

x = np.where(arr%2 == 1)

print(x)