any(), all() and empty iterable

Paul Rubin http
Sun Apr 12 08:09:54 EDT 2009


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)



More information about the Python-list mailing list