Hit enter after type your search item

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:

False

False

True

False

True

True

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:


The result:

False

True

False

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 pyton.org official website: click here for detailed reading

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:


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:


The output:

True

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.


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:


The output:

False

True

False

This div height required for enabling the sticky sidebar