[Tutor] Usefulness of BIFs all() and any()?

Richard D. Moores rdmoores at gmail.com
Tue Sep 25 13:55:47 CEST 2012


I was just perusing the Built-in Functions of Python 3.2 (<
http://docs.python.org/py3k/library/functions.html>) and was wondering
where would one ever use any() or all().

all(iterable)
Return True if all elements of the iterable are true (or if the iterable is
empty). Equivalent to:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

any(iterable)
Return True if any element of the iterable is true. If the iterable is
empty, return False. Equivalent to:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

Given a = [0, 1, 2, 3],

>>> all(a)
False
>>> any(a)
True

But so what? Could I get some better examples?

And why
>>> all([])
True
>>> any([])
False

Thanks,

Dick Moores
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120925/2f126a49/attachment.html>


More information about the Tutor mailing list