Signup/Sign In

Python any() method

Posted in Technology   LAST UPDATED: OCTOBER 23, 2019

    This post is dedicated to yet another built-in method in Python, the any() method.

    Python any() method

    This method returns True if any one of the values present inside the iterable is True, and False otherwise.

    Syntax of this method:

    any(iterable)

    It takes an iterables like list, tuples, dictionary or any other object that can be iterated through.

    Below are the rules that any() method uses to evaluate an iterable's data elements to True or False:

    1. If all values inside the iterables are True, it returns True.

    2. If all values inside the iterables are False, it returns False.

    3. If an empty iterable is passed as a parameter to the any method, it returns False.

    4. If one of the values inside the iterable is True and all other values are False, it returns True.

    5. If one of the values inside the iterable is False and all other values are True, it returns True.

    Now, let us look at how the any() method behaves when it is used with strings, lists, and dictionaries.




    1. any() with strings

    my_str = "Hi, this is StudyTonight"
    print(any(my_str))
    
    my_str = '000'  #0 is a False value, but 0 inside qutoes is a string, hence a true value
    print(any(my_str))
    
    my_str = ''  #all with empty string
    print(any(my_str))

    Output:

    True
    True
    False



    2. any() with lists

    my_list = [1, 3, 2, 5, 1.23, 5.67]
    print(any(my_list))
    
    my_list = [False, 0]  # A list that contains values which is equivalent to False
    print(any(my_list))
    
    my_list = [False, 0, 5.0]  # When one value is True and other values are False
    print(any(my_list))
    
    my_list = [1, 1.23, 5.67, 0]  # When one value is False, and all other values are True
    print(any(my_list))
    
    my_list = []   # When an empty list(iterable) is passed
    print(any(my_list))

    Output:

    True
    False
    True
    True
    False



    3. any() with dictionaries

    my_dict = {0: 'False', 1: 'False'}
    print(any(my_dict))
    
    my_dict = {1: 'True', 2: 'True'}
    print(any(my_dict))
    
    my_dict = {1: 'True', False: 0}
    print(any(my_dict))
    
    my_dict = {1: 'True', False: '0'}
    print(any(my_dict))
    
    my_dict = {}              # Empty dictionary passed as parameter to all
    print(any(my_dict))
    
    my_dict = {'0': 'True'}   # 0 (integer) is considered to be a False value, whereas '0' (string 0) 
                              # is considered to be a True value
    print(any(my_dict))

    Output:

    True
    True
    True
    True
    False
    True
    

    Note: The only difference between the any() and all() method can be determined by the name of the function itself. The all method returns true only if and only if all the values inside the iterable are True. On the other hand, any method returns True if any value inside the iterable is True.




    Conclusion:

    In this post, we understood how Python's built-in any method works with data structures and data types. To execute and see the code examples running, try executing the above code on our Python Playground available on our website.

    Author:
    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    PythonPython any()
    IF YOU LIKE IT, THEN SHARE IT

    RELATED POSTS