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 bool
s 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.
all([ v == 0 for v in values ])
is really very, very nice, clear, explicit and expressive. How much better could it be? – Beckerif itercmp(values, 0)
seems more meaningful because I'd know quickly that I am comparing all values with 0. Might just be my C background doing that though... – Enterpriseritercmp(values, 0)
more "meaningful" thanall([ v == 0 for v in values ])
. What? How is that possible? Please update your question with an explanation of howitercmp
is somehow more expressive thanall()
and0
is more expressive thanv == 0
. I assume you're not kidding, so please update the question with an explanation of how this is an improvement. – Beckerall
to be spelled "takes_an_iterable_of_bools_and_tests_all_of_them_for_True"? Is that what you're asking for? – Beckeralltrue()
would suffice, all would imply an iterable and true implies the test. What I'm looking for is a general function that can test for more than just true. I'll assume from your sarcastic answer that you don't know of one. – Enterpriserall
seems to be perfectly descriptive, I cannot understand your complaint. Still. SoallTrue
would be better thanall
? Is that what you're saying? What is this "general function" that tests for more than True. True/False is the result of all possible comparison operators, soall
applies to all possible logical tests. What are you asking for? – Beckerall()
is not perfectly descriptive.all(list)
doesn't mean anything and may or may not be correct,alltrue(list)
tells me that we are looking to see if all values inlist
are true. A general function would beall(list, True)
meaning that all items inlist
areTrue
orall(list, 0)
meaning that all items inlist
are0
orall(list, 'ok')
meaning that all items inlist
are'ok'
. The question is: "Is there a good, succinct/built-in way to see if all the values in an iterable are zeros?" I'm not sure where your confusion lies here. – Enterpriser==
. It doesn't seem sensible to ask for a function that applies to only one predicate when Python already has a function that already applies to all possible predicates. I don't get the "please limit me to just one special-purpose predicate" behind the question. What's the value in the limitation? – Beckerall()
only applies to "is true" predicate, you need to contort all the items in the list to have a truth value using a generator, which has the multiple predicate you are talking about. I am okay with using the generator, but I was asking if there is a way to express it without the generator. There is, in fact, a way to do that using numpy.array(), which probably build the generator automatically as part of__cmp__
. – Enterpriser