Python @ DjangoSpin

Python: Using builtin functions any() & all()

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Using any() & all() in Python

Using any() & all() in Python

Python provides two useful functions to check for true values of an iterable object.

The any() function returns True if any element of the iterable object is true, else returns False.

The all() function returns True if all elements of the iterable object are true, else returns False.

If the iterable object is empty, both these functions return False.

>>> help(any)
Help on built-in function any in module builtins:

any(...)
    any(iterable) -> bool
    
    Return True if bool(x) is True for any x in the iterable.
    If the iterable is empty, return False.

>>> help(all)
Help on built-in function all in module builtins:

all(...)
    all(iterable) -> bool
    
    Return True if bool(x) is True for all values x in the iterable.
    If the iterable is empty, return True.

## EXAMPLES ##
>>> any( [0, 1, 2, 3] )
True
>>> all( [0, 1, 2, 3] )
False

>>> any( [ '', 'a' , 'b', 'c' ] )
True
>>> all( [ '', 'a' , 'b', 'c' ] )
False

See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply