The all() function:
- Takes an argument (which is iterable) and returns True if all the elements of the iterable are True.
- If the given iterable is empty, it also returns True.
- If any element is False, the all() function returns False.
- The iterable can be a list, tuple, dictionary, string etc.
Syntax of using all() function
This is how all function is used in Python:
Now, I will show you using all() function with different iterators.
An example of all() with a list
In the first example, I am using a list of numbers, True/False values, and strings. See the code and output as I printed the output of all() function for different lists:
#A demo of all() function #List with numeric items 0 lst1 = [0, 0, 0] print(all(lst1))
#A list with numeric items 0 and 1 lst2 = [0, 1, 0, 1] print(all(lst2))
#List with numbers lst3 = [5, 10, 15, 20] print(all(lst3))
#List of strings with empty values lst4 = ['',''] print(all(lst4))
#string items list lst5 = ['python','all'] print(all(lst5))
#An empty list lst6 = [] print(all(lst6))
As such, 0 is evaluated as False, so all() function returned False for the first two lists.
The elements 5, 10, 15, and 20 were evaluated as True so all() function returned True for the third list.
Similarly, if you use the True/False values as the list items, this is how it results:
bool_lst1 = [True, False, False] bool_lst2 = [True, True, True] bool_lst3 = [False, False, False] print(all(bool_lst1)) print(all(bool_lst2)) print(all(bool_lst3))
The result:
False
True
False
The example of using a dictionary
In the following example, I used three dictionaries with various key/value pair combinations.
In the print function, I used all() and used those dictionaries as arguments.
See the values in dictionaries and their output:
dict_1 = {0 : "True", 1 : "True"} dict_2 = {1 : "False", 2 : "False"} dict_3 = {"name" : "Mike", "Salary" : 5000} print (all(dict_1)) print (all(dict_2)) print (all(dict_3))
The output:
You can see that the first dictionary’s result is False, as it contained a 0 key that resulted False. However, using the “False” as the value does not count.
Even, if we have ‘0’ value for any key, it will not return False. See the example below:
a_dict = {"Amount" : 0, "Income" : 0} print (all(a_dict))
The output:
An example of using a string as an argument
In this example, I will show you using different string objects are sent to the any() function and their results.
str1 = '' str2 = 'Some Text' str3 = 'False' str4 = '0' print (all(str1)) print (all(str2)) print (all(str3)) print (all(str4))
The results:
True
True
True
True
The example of using a tuple
As mentioned in the introductory part of the all() function, you may use any iterator as the argument in the all(), so tuple is no exception.
The following example shows using tuples in the all() function:
tup_1 = (0, 'True', 'False', 0.0) tup_2 = ('0', '0.0', 'False') tup_3 = (10, 20, False, True) print (all(tup_1)) print (all(tup_2)) print (all(tup_3))
The output:
False
True
False