Signup/Sign In
LAST UPDATED: JUNE 24, 2019

Python any() and all() Methods - Python Library Functions

Technology #Python#any()#all()

    In python language, the methods any() and all() are built-in library functions which are quite opposite to each other in functioning.

    There are only two cases in which both any() and all() methods return same output, they are:

    Case 1: When all the values in the iterable are TRUE, both methods will return TRUE.

    Case 2: When all the values in the iterable are FALSE, both methods return FALSE.

    In any other possible usecase, the output returned by these two methods are unique. Also, we use these methods with iterables like list, tuples, dictionary etc.

    any() and all() functions in python




    any() Function:

    The method any() returns TRUE, if any of the items in the iterable are TRUE. It returns FALSE, if an empty argument is passed or if all items in the iterable are FALSE.

    any() function executes just like the OR operation on the provided iterables.


    Syntax:

    any(X)
    # where X is an iterable argument that can be list or dictionary or string etc.

    Just to understand the any() function's working let's see the OR operation truth table.


    OR Operation Truth Table:

    If in an Iterable

    any() returns

    All items are TRUE

    TRUE

    All items are FALSE

    FALSE

    One item is TRUE while others are FALSE

    TRUE

    One item is FALSE while others are TRUE

    TRUE

    If there are no items in the itereable

    FALSE




    all() Function:

    The method all() returns FALSE, if any of the items in the iterable are FALSE. It returns TRUE, if an empty argument is passed or if all items in the iterable are TRUE.

    all() function executes just like the AND operation on the provided iterables.


    Syntax:

    all(X)
    # where X is an iterable argument that can be list or dictionary or string etc.

    AND Operation Truth Table:

    If in an Iterable

    all() returns

    All items are TRUE

    TRUE

    All items are FALSE

    FALSE

    One item is TRUE while others are FALSE

    FALSE

    One item is FALSE while others are TRUE

    FALSE

    If there are no items in the itereable

    TRUE

    Let's have some programmatic examples using the above two methods for better understanding.




    Using any() and all() functions with Python Lists:




    Using any() and all() functions with Python Dictionaries:




    Using any() and all() functions with Python Tuples:




    Conclusion:

    Pheww! This is it. I hope that you enjoyed the post and learned about the any() & all() library functions with three different examples using lists, tuples and dictionaries.

    A small task for you, try these functions by passing other type arguments like strings, sets etc. and share the output along with the program and we will discuss about it in the comment section.

    If you feel that this post is useful, please share it with your friends and colleagues.

    Thanks for reading it till the end.

    I am a self-taught Blogger and Programmer. I have good sort of knowledge in Technology, PHP, Javascript, HTML, CSS and Python.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS