--- canonical_url: https://www.scaler.com/topics/all-in-python/ title: All in Python - Scaler Topics description: Learn about all() in Python by Scaler Topics. This article covers in detail the inbuilt function all() in python. Read to know more. author: Srishti Trivedi category: Python amphtml: https://www.scaler.com/topics/all-in-python/amp/ publish_date: 2022-02-25 --- :::section{.abstract} The `all()` function in [python](https://www.scaler.com/topics/python/) is an inbuilt function that evaluates whether all elements within an iterable object are truthy. It returns True if all elements are true, otherwise False. Iterable objects such as lists, tuples, sets, and dictionaries can be passed as arguments to all(). Even when the iterable is empty, all() yields True. Conversely, if any element in the iterable is falsy, all() returns False. ::: :::section{.main} ## Example ```python # Consider two boolean lists having values as below. boolean_list_1 = [1, True, 1] boolean_list_2 = [0, False, 5] # Checking if the above lists with values result in output as TRUE or FALSE with all() function in python output_1 = all(boolean_list_1) output_2 = all(boolean_list_2) # Printing the output of the boolean lists print('output_1 = ', output_1) print('output_2 = ', output_2) ``` **Output:** ```python output_1 = True output_2 = False ``` ::: :::section{.main} ## Syntax The syntax of the ``all()`` in python is as follows: ```python all(iterable) ``` ::: :::section{.main} ## Parameters The ``all() function`` in python accepts a single parameter: **iterable:** The iterable is considered an object such as a list, set, dictionary, tuple, etc. ::: :::section{.main} ## Return Values The return values of ``all()`` in python are: ``TRUE or FALSE``. **True -** The output is returned ``TRUE`` if all the elements in an iterable are ``TRUE``. Even if the iterable object is empty while executing the ``all() function`` we get the output as ``TRUE``. **False -** The output is returned ``FALSE`` if any elements in an iterable are false. For a better glance at all the possible scenarios, let us dive deeper below: | Possible Scenarios | Output Values | |:-----------------------------------------:|:---------------:| | All iterable are true | TRUE | | All iterable are false | FALSE | | One iterable is true (others are false) | FALSE | | One iterable is false (others are true) | FALSE | | Empty Iterable | TRUE | ::: :::section{.main} ## Exceptions As far as exceptions for ``all()`` in python are considered, we do need to keep in mind the following points: 1. Whenever we are dealing with ``all() function`` with Dictionaries, we need to remember that the keys are evaluated for the scenarios mentioned above. It means that if all the keys in the dictionary are `true`, then the output is `true`. Otherwise, it returns false. 2. Whenever we deal with integers like ``0, 1, 2...`` in strings as we keep them in ``""`` (inverted commas), the output is always ``TRUE``. ::: :::section{.main} ## More Examples ### 1. `all()` with Lists ```python # Each list considers the five scenarios which we just understood theoretically # list_1 explains the First Scenario - All values are TRUE list_1 = [9, 1, 8, 3, True] result_1 = all(list_1) print("The OUTPUT of the list_1 is:",result_1) # list_2 explains the Second Scenario - All values are FALSE list_2 = [ False , 0] result_2 = all(list_2) print("The OUTPUT of the list_2 is:",result_2) # list_3 explains the Third Scenario - One value is true (others are false) list_3 = [False, 4 , 0] result_3 = all(list_3) print("The OUTPUT of the list_3 is:",result_3) # list_4 explains the Fourth Scenario - One value is false (others are true) list_4 = [8, 9, 2, 0] result_4 = all(list_4) print("The OUTPUT of the list_4 is:",result_4) # list_5 explains the Fifth Scenario - Empty Iterable list_5 = [] result_5 = all(list_5) print("The OUTPUT of the list_5 is:",result_5) ``` **Output:** ```python The OUTPUT of the list_1 is: True The OUTPUT of the list_2 is: False The OUTPUT of the list_3 is: False The OUTPUT of the list_4 is: False The OUTPUT of the list_5 is: True ``` ### 2. `all()` with Tuples ```python # Each tuple considers the five scenarios which we just understood theoretically # tuple_1 explains the First Scenario - All values are TRUE tuple_1 = (9, 1, 8, 3, True) result_1 = all(tuple_1) print("The OUTPUT of the tuple_1 is:",result_1) # tuple_2 explains the Second Scenario - All values are FALSE tuple_2 = ( False , 0 ) result_2 = all(tuple_2) print("The OUTPUT of the tuple_2 is:",result_2) # tuple_3 explains the Third Scenario - One value is true (others are false) tuple_3 = (False, 4 , 0) result_3 = all(tuple_3) print("The OUTPUT of the tuple_3 is:",result_3) # tuple_4 explains the Fourth Scenario - One value is false (others are true) tuple_4 = (8, 9, 2, 0) result_4 = all(tuple_4) print("The OUTPUT of the tuple_4 is:",result_4) # tuple_5 explains the Fifth Scenario - Empty Iterable tuple_5 = () result_5 = all(tuple_5) print("The OUTPUT of the tuple_5 is:",result_5) ``` **Output:** ```python The OUTPUT of the tuple_1 is: True The OUTPUT of the tuple_2 is: False The OUTPUT of the tuple_3 is: False The OUTPUT of the tuple_4 is: False The OUTPUT of the tuple_5 is: True ``` ### 3. `all()` with Sets ```python # Each set considers the five scenarios which we just understood theoretically # set_1 explains the First Scenario - All values are TRUE set_1 = {9, 1, 8, 3, True} result_1 = all(set_1) print("The OUTPUT of the set_1 is:",result_1) # set_2 explains the Second Scenario - All values are FALSE set_2 = { False , 0 } result_2 = all(set_2) print("The OUTPUT of the set_2 is:",result_2) # set_3 explains the Third Scenario - One value is true (others are false) set_3 = {False, 4 , 0} result_3 = all(set_3) print("The OUTPUT of the set_3 is:",result_3) # set_4 explains the Fourth Scenario - One value is false (others are true) set_4 = {8, 9, 2, 0} result_4 = all(set_4) print("The OUTPUT of the set_4 is:",result_4) # set_5 explains the Fifth Scenario - Empty Iterable set_5 = {} result_5 = all(set_5) print("The OUTPUT of the set_5 is:",result_5) ``` **Output:** ```python The OUTPUT of the set_1 is: True The OUTPUT of the set_2 is: False The OUTPUT of the set_3 is: False The OUTPUT of the set_4 is: False The OUTPUT of the set_5 is: True ``` ### 4. `all()` with Dictionaries ::: :::section{.tip} **PRO TIP:** Whenever we are dealing with ``all() function`` with Dictionaries, we need to remember that the keys are evaluated for the scenarios mentioned above. It means that if all the keys of the dictionary are `true`, then the output is `true` , or else it returns false. ::: :::section{.main} ```python # Each dictionary considers the five scenarios we just understood theoretically # dict_1 explains the First Scenario - All values are TRUE dict_1 = {1: 'True', 7: 'True'} result_1 = all(dict_1) print("The OUTPUT of the dict_1 is:",result_1) # dict_2 explains the Second Scenario - All values are FALSE dict_2 = { 0: 'False', 1: 'False'} result_2 = all(dict_2) print("The OUTPUT of the dict_2 is:",result_2) # dict_3 explains the Third Scenario - One value is true (others are false) dict_3 = {1: 'True', 3: 'False' ,0: 'False' } result_3 = all(dict_3) print("The OUTPUT of the dict_3 is:",result_3) # dict_4 explains the Fourth Scenario - One value is false (others are true) dict_4 = {0: 'False',1: 'True', 4: 'True' } result_4 = all(dict_4) print("The OUTPUT of the dict_4 is:",result_4) # dict_5 explains the Fifth Scenario - Empty Iterable dict_5 = {} result_5 = all(dict_5) print("The OUTPUT of the dict_5 is:",result_5) ``` **Output:** ```python The OUTPUT of the dict_1 is: True The OUTPUT of the dict_2 is: False The OUTPUT of the dict_3 is: False The OUTPUT of the dict_4 is: False The OUTPUT of the dict_5 is: True ``` ### 5. How `all()` works for strings Whenever we are dealing with ``all() function`` with the iterable object as strings, we can have the following two scenarios majorly: 1. ``NON- EMPTY`` Strings which give output as ``TRUE``. 2. ``EMPTY`` Strings which give output as ``TRUE``. ::: :::section{.tip} **PRO TIP:** Whenever we deal with integers like ``0, 1, 2..``in strings as we keep them in ``""`` (inverted commas), the output is always TRUE. ::: Let us dive into the example below to understand the concept explained above. ```python # Each string considers the five scenarios we just understood theoretically # string_1 explains the First Scenario - NON- EMPTY Strings, giving output as TRUE. string_1 = "Scaler Topics!" result_1 = all(string_1) print("The OUTPUT of the string_1 is:",result_1) # string_2 explains the Second Scenario - EMPTY Strings, which gives output as TRUE. string_2 = "" result_2 = all(string_2) print("The OUTPUT of the string_2 is:",result_2) # string_3 explains the Third Scenario - Integers inside strings string_3 = "012" result_3 = all(string_3) print("The OUTPUT of the string_3 is:",result_3) ``` **Output:** ```python The OUTPUT of the string_1 is: True The OUTPUT of the string_2 is: True The OUTPUT of the string_3 is: True ``` ::: :::section{.main} ## Conclusion 1. The ``all()`` in Python returns the output ``TRUE`` if all elements in the given iterable object are true. 2. It works with various iterable objects, including lists, tuples, sets, and dictionaries. The function considers dictionary keys for evaluation, not values. 3. The function evaluates elements in a boolean context. Non-zero, non-empty, or non-`False` values are considered `True`. 4. For strings and numbers in strings, `all()` considers empty strings and strings with non-zero numbers as `True`. ::: :::section{.main} ## See Also 1. [filter() in python](https://www.scaler.com/topics/filter-function-in-python/) 2. [any() in python](https://www.scaler.com/topics/any-in-python/) :::