Signup/Sign In
LAST UPDATED: MARCH 15, 2021

Python all() method

    There are innumerable built-in methods in Python which fulfill a wide variety of purposes in the world of technology. Today, we will explore the all method in Python.

    Python all() method

    The all() method is a built-in Python method that checks whether the values inside an iterable are true or false.

    Syntax of this method:

    all(iterable)

    It returns true if all the values present inside an iterable (like list, tuple, dictionary) are true and returns false otherwise. It takes a single parameter which could be a list or tuple or dictionary or any other iterable that contains data elements.

    Below are the rules which the all method uses while evaluating an iterable:

    1. If all values are true, return True.

    2. If all values are false, return False.

    3. If an empty iterable is passed as a parameter, return True.

    4. If only one of the values inside the iterable is true and all other values are false, return False.

    5. If only one of the values inside the iterable is false and all other values are true, return False.

    Now let us look at how the all method works with strings, lists, and dictionaries with the help of separate examples.

    1. all() with strings

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

    Output:

    True
    True
    True

    2. all() with list object

    As mentioned earlier, an empty list passed as a parameter to all method returns True. Understand the rules which the all method follows to determine the output of all method when it is used with different values in a list data structure.

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

    Output:

    True
    False
    False
    False
    True

    3. all() with dictionary object

    Execute the below lines of code separately to understand the output.

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

    Output:

    False
    True
    False
    False
    True
    True

    Conclusion:

    In this post, we understood how the all() method works with different data structures and types. To execute and see the code examples running, try executing the above code on our Python Playground available on our website.

    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.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS