Python | Check if all values in numpy are zero Last Updated : 13 Mar, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report Given a numpy array, the task is to check whether the numpy array contains all zeroes or not. Let's discuss few ways to solve the above task. Method #1: Getting count of Zeros using numpy.count_nonzero() Python3 # Python code to demonstrate # to count the number of elements # in numpy which are zero import numpy as np ini_array1 = np.array([1, 2, 3, 4, 5, 6, 0]) ini_array2 = np.array([0, 0, 0, 0, 0, 0]) # printing initial arrays print("initial arrays", ini_array1) print(ini_array2) # code to find whether all elements are zero countzero_in1 = np.count_nonzero(ini_array1) countzero_in2 = np.count_nonzero(ini_array2) # printing result print("Number of non-zeroes in array1 : ", countzero_in1) print("Number of non-zeroes in array2 : ", countzero_in2) Output:initial arrays [1 2 3 4 5 6 0] [0 0 0 0 0 0] Number of non-zeroes in array1 : 6 Number of non-zeroes in array2 : 0 Time complexity: O(n)The count_nonzero() function iterates through the entire input array to count the number of non-zero elements. Auxiliary space complexity: O(1)The code only uses a constant amount of extra space to store the input arrays and the count of non-zero elements. Method #2: Using numpy.any() Python3 # Python code to check that whether # all elements in numpy are zero import numpy as np ini_array1 = np.array([1, 2, 3, 4, 5, 6, 0]) ini_array2 = np.array([0, 0, 0, 0, 0, 0]) # printing initial arrays print("initial arrays", ini_array1) # code to find whether all elements are zero countzero_in1 = not np.any(ini_array1) countzero_in2 = not np.any(ini_array2) # printing result print("Whole array contains zeroes in array1 ?: ", countzero_in1) print("Whole array contains zeroes in array2 ?: ", countzero_in2) Output:initial arrays [1 2 3 4 5 6 0] Whole array contains zeroes in array1 ?: False Whole array contains zeroes in array2 ?: True Method #3: Using numpy.count_nonzero Check if all values in array are zero using np.count_nonzero() Python3 import numpy as np # Initialize the numpy arrays array1 = np.array([1, 2, 3, 4, 5, 6, 0]) array2 = np.array([0, 0, 0, 0, 0, 0]) # Check if all values in array1 are zero if np.count_nonzero(array1) == 0: print("Array 1: all values are zero? True") else: print("Array 1: all values are zero? False") # Check if all values in array2 are zero if np.count_nonzero(array2) == 0: print("Array 2: all values are zero? True") else: print("Array 2: all values are zero? False") #This code is contributed by Edula Vinay Kumar Reddy This will give output as: Array 1: all values are zero? FalseArray 2: all values are zero? True Comment More infoAdvertise with us Next Article Python | Check if all values in numpy are zero G garg_ak0109 Follow Improve Article Tags : Python Python numpy-program Practice Tags : python Similar Reads Check For NaN Values in Python In data analysis and machine learning, missing or NaN (Not a Number) values can often lead to inaccurate results or errors. Identifying and handling these NaN values is crucial for data preprocessing. Here are five methods to check for NaN values in Python. What are Nan Values In In Python?In Python 2 min read Create a Numpy array filled with all zeros - Python In this article, we will learn how to create a Numpy array filled with all zeros, given the shape and type of array. We can use Numpy.zeros() method to do this task. Let's understand with the help of an example:Pythonimport numpy as np # Create a 1D array of zeros with 5 elements array_1d = np.zeros 2 min read How to Create Array of zeros using Numpy in Python numpy.zeros() function is the primary method for creating an array of zeros in NumPy. It requires the shape of the array as an argument, which can be a single integer for a one-dimensional array or a tuple for multi-dimensional arrays. This method is significant because it provides a fast and memory 4 min read How to check whether specified values are present in NumPy array? Sometimes we need to test whether certain values are present in an array. Using Numpy array, we can easily find whether specific values are present or not. For this purpose, we use the "in" operator. "in" operator is used to check whether certain element and values are present in a given sequence an 2 min read Check If Value Is Int or Float in Python In Python, you might want to see if a number is a whole number (integer) or a decimal (float). Python has built-in functions to make this easy. There are simple ones like type() and more advanced ones like isinstance(). In this article, we'll explore different ways to do this efficiently. Using type 2 min read Like