[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().

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

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)
>>> any(a)

But so what? Could I get some better examples?

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


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