Python all() function with Examples

Python all() function is one of the built-in functions. It takes iterable as an argument and returns True if all elements of the iterable are true or it’s empty.

Python all() function

Python all() function is a utility method and shortcut to below function.

Let’s look at some of the examples of python all() function.

Python all() example with boolean


Python all() with empty iterable


Python all() with list of strings


When we want an object boolean value, python looks for __bool__ function in the object.

If __bool__ function is not defined, then len() function is called if it’s defined. The object boolean value is considered as True if len() output is non-zero.

If a class defines neither __len__() nor __bool__() functions, all its instances are considered True.

Python 3 uses __bool__ function to check object boolean value, if you are using python 2.x then you have to implement __nonzero__ function.

Python all() with custom objects

Let’s test above explanation with a custom class. We will create a custom Person class and use its objects in the list and call all() function on it.


Since our object doesn’t have __len__() and __bool__() function defined, it’s boolean value is True.

Let’s go ahead and define __len__() function for the Person class as below.

Now the output of earlier code snippets will be:

Notice that len() function is getting called for each object when all() is used with the list of Person objects.

Now let’s define __bool__ function for the Person class and see what happens with the above code.


It’s clear from the output that if __bool__ function is defined, then it’s used for getting the python object boolean value. Notice that second list all() function output is False because ‘A’ length is less than 3.

That’s all for python all() function examples.

You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: