any(), all() and empty iterable
Arnaud Delobelle
arnodel at googlemail.com
Sun Apr 12 09:27:43 EDT 2009
Paul Rubin <http://phr.cx@NOSPAM.invalid> writes:
> Tim Chase <python.list at tim.thechases.com> writes:
>> > Return True if all elements of the iterable are
>> > true. ...
>> Then I'd say the comment is misleading. An empty list has no item
>> that is true (or false), yet it returns true.
>
> The comment is correct. "All the items of the iterable are true"
> means EXACTLY the same thing as "there are no items of the iterable
> that are false". The empty list has no false items. Therefore
> all(empty_list) = True is the correct behavior.
>
>
> Another possible implementation:
>
> import operator,itertools
> def all(xs):
> return reduce(operator.and_, itertools.imap(bool, xs), True)
A contest! My entry:
def all(iterable):
return not sum(not x for x in iterable)
--
Arnaud
More information about the Python-list
mailing list