Check if all elements in a List are zero in Python

In this article, we will discuss different ways to check if all values are zeros in a List.

Table Of Contents

Using for-loop 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 non-zero 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.

Advertisements

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

   

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 sought-after 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.

Join a LinkedIn Community of Python Developers

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top