Check if all values of iterable are zero

PythonIterable

Python Problem Overview


Is there a good, succinct/built-in way to see if all the values in an iterable are zeros? Right now I am using all() with a little list comprehension, but (to me) it seems like there should be a more expressive method. I'd view this as somewhat equivalent to a memcmp() in C.

values = (0, 0, 0, 0, 0)
# Test if all items in values tuple are zero
if all([ v == 0 for v in values ]) :
    print 'indeed they are'

I would expect a built-in function that does something like:

def allcmp(iter, value) :
    for item in iter :
        if item != value :
            return False
    return True

Does that function exist in python and I'm just blind, or should I just stick with my original version?

Update

I'm not suggesting that allcmp() is the solution. It is an example of what I think might be more meaningful. This isn't the place where I would suggest new built-ins for Python.

In my opinion, all() isn't that meaningful. It doesn't express what "all" is checking for. You could assume that all() takes an iterable, but it doesn't express what the function is looking for (an iterable of bools that tests all of them for True). What I'm asking for is some function like my allcmp() that takes two parameters: an iterable and a comparison value. I'm asking if there is a built-in function that does something similar to my made up allcmp().

I called mine allcmp() because of my C background and memcmp(), the name of my made up function is irrelevant here.

Python Solutions


Solution 1 - Python

Use generators rather than lists in cases like that:

all(v == 0 for v in values)

Edit:

all is standard Python built-in. If you want to be efficient Python programmer you should know probably more than half of them (http://docs.python.org/library/functions.html). Arguing that alltrue is better name than all is like arguing that C while should be call whiletrue. Is subjective, but i think that most of the people prefer shorter names for built-ins. This is because you should know what they do anyway, and you have to type them a lot.

Using generators is better than using numpy because generators have more elegant syntax. numpy may be faster, but you will benefit only in rare cases (generators like showed are fast, you will benefit only if this code is bottleneck in your program).

You probably can't expect nothing more descriptive from Python.

PS. Here is code if you do this in memcpm style (I like all version more, but maybe you will like this one):

list(l) == [0] * len(l)

Solution 2 - Python

If you know that the iterable will contain only integers then you can just do this:

if not any(values):
    # etc...

Solution 3 - Python

If values is a numpy array you can write

import numpy as np
values = np.array((0, 0, 0, 0, 0))
all(values == 0)

Solution 4 - Python

The any() function may be the most simple and easy way to achieve just that. If the iterable is empty,e.g. all elements are zero, it will return False.

values = (0, 0, 0, 0, 0)
print (any(values)) # return False

Solution 5 - Python

The built-in set is given an iterable and returns a collection (set) of unique values.

So it can be used here as:

set(it) == {0}
  • assuming it is the iterable
  • {0} is a set containing only zero

More info on python set-types-set-frozenset here in docs.

Solution 6 - Python

I prefer using negation:

all(not v for v in values)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmjschultzView Question on Stackoverflow
Solution 1 - PythonTomasz WysockiView Answer on Stackoverflow
Solution 2 - PythonMark ByersView Answer on Stackoverflow
Solution 3 - PythonJohn La RooyView Answer on Stackoverflow
Solution 4 - PythonHarryView Answer on Stackoverflow
Solution 5 - PythonBloop93View Answer on Stackoverflow
Solution 6 - PythonNic ScozzaroView Answer on Stackoverflow