Python all() function

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.

Note:  You may also like to learn about Python any function that returns True if any of the element is True.

Syntax of using all() function

This is how all function is used in Python:

all( iterable )

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]



#A list with numeric items 0 and 1

lst2 = [0, 1, 0, 1]



#List with numbers

lst3 = [5, 10, 15, 20]



#List of strings with empty values

lst4 = ['','']



#string items list

lst5 = ['python','all']



#An empty list

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 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]




The result:




[stextbox id=’info’ bwidth=’2′ color=’f7f5f5′ bcolor=’d9a85f’ bgcolor=’1e2c45′ bgcolorto=’88b0b8′]Truth value testing: You might be interested in how Python works as for as testing the truth value for object. If you are not sure then this is a recommended reading in the official website: click here for detailed reading[/stextbox]

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:

Python all

You see, the first dictionary’s result is False, as it contained a 0 key that resulted False. However, using the “False” as 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:





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:




Author - Atiq Zia

Atiq is the writer at, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!