Find indices of elements equal to zero in a NumPy array
Last Updated :
08 Mar, 2023
Sometimes we need to find out the indices of all null elements in the array. Numpy provides many functions to compute indices of all null elements.
Method 1: Finding indices of null elements using numpy.where()
This function returns the indices of elements in an input array where the given condition is satisfied.
Syntax :
numpy.where(condition[, x, y])
When True, yield x, otherwise yield y
Python3
# importing Numpy package
import numpy as np
# creating a 1-D Numpy array
n_array = np.array([1, 0, 2, 0, 3, 0, 0, 5,
6, 7, 5, 0, 8])
print("Original array:")
print(n_array)
# finding indices of null elements using np.where()
print("\nIndices of elements equal to zero of the \
given 1-D array:")
res = np.where(n_array == 0)[0]
print(res)
Output:

Time complexity: O(n) - where n is the size of the array
Auxiliary space: O(k) - where k is the number of null elements in the array, as we are storing their indices in a separate array.
Method 2: Finding indices of null elements using numpy.argwhere()
This function is used to find the indices of array elements that are non-zero, grouped by element.
Syntax :
numpy.argwhere(arr)
Python3
# importing Numpy package
import numpy as np
# creating a 3-D Numpy array
n_array = np.array([[0, 2, 3],
[4, 1, 0],
[0, 0, 2]])
print("Original array:")
print(n_array)
# finding indices of null elements
# using np.argwhere()
print("\nIndices of null elements:")
res = np.argwhere(n_array == 0)
print(res)
Output:

The time complexity of the code is O(m * n) where m and n are the dimensions of the 3-D Numpy array .
The auxiliary space complexity of the code is O(k) where k is the number of null elements in the 3-D Numpy array .
Method 3: Finding the indices of null elements using numpy.nonzero()
This function is used to Compute the indices of the elements that are non-zero. It returns a tuple of arrays, one for each dimension of arr, containing the indices of the non-zero elements in that dimension.
Syntax:
numpy.nonzero(arr)
Python3
# importing Numpy package
import numpy as np
# creating a 1-D Numpy array
n_array = np.array([1, 10, 2, 0, 3, 9, 0,
5, 0, 7, 5, 0, 0])
print("Original array:")
print(n_array)
# finding indices of null elements using
# np.nonzero()
print("\nIndices of null elements:")
res = np.nonzero(n_array == 0)
print(res)
Output:

The time complexity is O(n), where n is the number of elements in the input array.
The auxiliary space complexity is O(k), where k is the number of null elements in the input array.
Method 4: Using numpy.extract() method
Use the numpy.extract() method. This method returns an array of values that satisfy a certain condition. In this case, we can use it to extract the indices of elements that are equal to zero.
Python3
# importing Numpy package
import numpy as np
# creating a 1-D Numpy array
n_array = np.array([1, 0, 2, 0, 3, 0, 0, 5,
6, 7, 5, 0, 8])
print("Original array:")
print(n_array)
# finding indices of null elements using np.extract()
print("\nIndices of elements equal to zero of the \
given 1-D array:")
res = np.extract(n_array == 0, np.arange(len(n_array)))
print(res)
Output:
Original array:
[1 0 2 0 3 0 0 5 6 7 5 0 8]
Indices of elements equal to zero of the given 1-D array:
[ 1 3 5 6 11]
Time complexity: O(n), where n is the length of the input array.
Auxiliary space: O(m), where m is the number of elements in the input array that are equal to zero.
Similar Reads
Test whether the elements of a given NumPy array is zero or not in Python In numpy, we can check that whether none of the elements of given array is zero or not with the help of numpy.all() function. In this function pass an array as parameter. If any of one element of the passed array is zero then it returns False otherwise it returns True boolean value. Syntax: numpy.al
2 min read
How to check whether the elements of a given NumPy array is non-zero? In NumPy with the help of any() function, we can check whether any of the elements of a given array in NumPy is non-zero. We will pass an array in the any() function if it returns true then any of the element of the array is non zero if it returns false then all the elements of the array are zero. S
1 min read
Counting the number of non-NaN elements in a NumPy Array In this article, we are going to see how to count the number of non-NaN elements in a NumPy array in Python. NAN: It is used when you don't care what the value is at that position. Maybe sometimes is used in place of missing data, or corrupted data. Method 1: Using Condition In this example, we wil
3 min read
How to find the Index of value in Numpy Array ? In this article, we are going to find the index of the elements present in a Numpy array.Using where() Methodwhere() method is used to specify the index of a particular element specified in the condition.Syntax: numpy.where(condition[, x, y])Example 1: Get index positions of a given valueHere, we fi
5 min read
NumPy| How to get the unique elements of an Array To find unique elements of an array we use the numpy.unique() method of the NumPy library in Python. It returns unique elements in a new sorted array. Example: Python3 import numpy as np arr = np.array([1, 2, 3, 1, 4, 5, 2, 5]) unique_elements = np.unique(arr) print(unique_elements) Output: [1 2 3 4
2 min read