John Posner <jjpos...@snet.net> writes: > jazbees wrote: > >> I'm surprised to see that the use of min and max for element-wise >> comparison with lists has not been mentioned. When fed lists of True/ >> False values, max will return True if there is at least one True in >> the list, while min will return False if there is at least one False. >> Going back to the OP's initial example, one could wrap a min check on >> each list inside another min. > > I agree with Duncan Booth that any() is equivalent to -- and better > than -- max() for handling boolean values. But what boolean-oriented > function is equivalent to min()? How about this, which returns the > negation of the min() result: > > def at_least_one_false(value_list): > """ > return True if at least one value in value_list is logically FALSE > """ > return any( [not val for val in value_list] ) > > This works, but the short-circuit feature of any() is undermined by > the process-the-whole-sequence behavior of the list comprehension. So, > let's delete the square brackets, converting the list comprehension > into a generator expression: > > return any( not val for val in value_list )
This is the same as: return not all(value_list) > According to timeit.Timer.timeit(), this change improves performance > from 14.59 seconds to 10.49 seconds, with a long value_list: > > [True]*20 + [False] + [True]*30 > > But the generator expression produces worse performance with a shorter > value_list: > > [True]*2 + [False] + [True]*3 -- Arnaud -- http://mail.python.org/mailman/listinfo/python-list