In this article, we will discuss different ways to check if all values are zeros in a List.
Table Of Contents
Using forloop to check if List has only Zeros
Iterate over all the elements of a list using a for loop, and for each element check if it is zero or not. As soon as you find an element that is not zero, break the loop, because it means that there is atleast one nonzero value in the list . But also make sure that list is not empty. Let’s see a complete example,
listOfNumbers = [0, 0, 0, 0, 0] result = True for num in listOfNumbers: if num != 0: result = False break # Check if all elements in List are zeros if len(listOfNumbers) > 0 and result: print('Yes, List contains all zeros') else: print('No, List does not contains all zeros')
Output:
Yes, List contains all zeros
We have verified that there were only zeros in the List.
Using all() method to check if List has only Zeros
Python provides a function all(). It accepts an iterable sequence as an argument and returns True if all the elements in that sequence evaluates to True. Now, to check if List has only zeros, we will create a boolean list of same size. A value in the boolean list will be True if the corresponding value in the original list is zero. Now, pass this boolean list to the any() function. If it returns True, then it means list has only zeros. But also make sure that list is not empty. Let’s see a complete example,
listOfNumbers = [0, 0, 0, 0, 0] # Check if all elements in List are zeros if all(num == 0 for num in listOfNumbers) and len(listOfNumbers) > 0: print('Yes, List contains all zeros') else: print('No, List does not contains all zeros')
Output:
Yes, List contains all zeros
We have verified that there were only zeros in the List.
Using NumPy to check if List has only Zeros
The NumPy module has a function all(). It accepts an array like sequence as an argument and returns True if all the elements in that sequence evaluates to True. Now, to check if List has only Zeros or not, we will create a boolean NumPy Array of same size. A value in the boolean array will be True, if the corresponding value in the original list is zero. Then pass our boolean NumPy Array object to the numpy.all() function, and if it returns True, then it means our list has only zeros. Let’s see an example,
import numpy as np listOfNumbers = [0, 0, 0, 0, 0] # Create a NumPy Array from list numbers = np.array(listOfNumbers) # Check if all values in array are zeros if np.all(numbers == 0) and len(listOfNumbers) > 0: print('Yes, List contains all zeros') else: print('No, List does not contains all zeros')
Output:
Yes, List contains all zeros
We have verified that there were only zeros in the List.
Using Set to check if List has only Zeros
In Python, a Set is a kind of data structure that contains only unique elements. So, we can convert our list to a Set and check following conditions,
 Set size should be 1.
 First element of set should be equal to first element of list and that should evaluate to 0.
If both the conditions are True, then it means our list has only zeros. We can verify both the conditions by comparing the set with {0}.
Let’s see an example,
listOfNumbers = [0, 0, 0, 0, 0] # Check if all values in List are zeros if set(listOfNumbers) == {0}: print('Yes, List contains all zeros') else: print('No, List does not contains all zeros')
Output:
Yes, List contains all zeros
We have verified that there were only zeros in the List.
Summary
We learned how to verify of a List has only Zeros in Python. Thanks.
Pandas Tutorials Learn Data Analysis with Python

Pandas Tutorial Part #1  Introduction to Data Analysis with Python

Pandas Tutorial Part #2  Basics of Pandas Series

Pandas Tutorial Part #3  Get & Set Series values

Pandas Tutorial Part #4  Attributes & methods of Pandas Series

Pandas Tutorial Part #5  Add or Remove Pandas Series elements

Pandas Tutorial Part #6  Introduction to DataFrame

Pandas Tutorial Part #7  DataFrame.loc[]  Select Rows / Columns by Indexing

Pandas Tutorial Part #8  DataFrame.iloc[]  Select Rows / Columns by Label Names

Pandas Tutorial Part #9  Filter DataFrame Rows

Pandas Tutorial Part #10  Add/Remove DataFrame Rows & Columns

Pandas Tutorial Part #11  DataFrame attributes & methods

Pandas Tutorial Part #12  Handling Missing Data or NaN values

Pandas Tutorial Part #13  Iterate over Rows & Columns of DataFrame

Pandas Tutorial Part #14  Sorting DataFrame by Rows or Columns

Pandas Tutorial Part #15  Merging or Concatenating DataFrames

Pandas Tutorial Part #16  DataFrame GroupBy explained with examples
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most soughtafter professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.