all() in Python
The all() function in 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.
Syntax of all() in Python
The syntax of the all() in python is as follows:
Parameters of all() in Python
The all() function in python accepts a single parameter:
iterable: The iterable is considered an object such as a list, set, dictionary, tuple, etc.
Return Values of all() in Python
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 |
Example
Output:
More Examples
Example 1: all() in Python with Lists
Output:
Example 2: all() in Python with Tuples
Output:
Example 3: all() in Python with Sets
Output:
Example 4: all() in Python with Dictionaries
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.
Output:
Example 5: all() in Python with Strings
Whenever we are dealing with all() function with the iterable object as strings, we can have the following two scenarios majorly:
- NON- EMPTY Strings which give output as TRUE.
- EMPTY Strings which give output as TRUE.
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.
Output:
Example 6: all() function with Condition
Output:
Exceptions of all() in Python
As far as exceptions for all() in python are considered, we do need to keep in mind the following points:
- 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.
- Whenever we deal with integers like 0, 1, 2... in strings as we keep them in "" (inverted commas), the output is always TRUE.
Conclusion
- The all() in Python returns the output TRUE if all elements in the given iterable object are true.
- It works with various iterable objects, including lists, tuples, sets, and dictionaries. The function considers dictionary keys for evaluation, not values.
- The function evaluates elements in a boolean context. Non-zero, non-empty, or non-False values are considered True.
- For strings and numbers in strings, all() considers empty strings and strings with non-zero numbers as True.