Python All Function

In this tutorial, I will teach you about builtin python all function. for a better understanding of concepts, I will also give you relevant examples. Let’s start with the definition.

Definition :

Python all function takes an iterable(list, tuple, set, dictionary) as an argument and returns True if all the elements inside iterable are True and return False if any element inside the iterable is False. It also returns True if iterable is empty.

Syntax :

all(iterable)

Source Code :

Python all Function is the utility function and behaves exactly like the below function.

def all(iterable):
    for e in iterable:
        if not e:
            return False
    return True

Code Examples

To fully understand the concept let’s look at some examples.

Example 1 :

Let’s see how the function behaves with the list and booleans.

list1 = [True,True,True]
print(all(list1))

list2 = [True,False,True]
print(all(list2))

list3 = [False,False,False]
print(all(list3))

list4 = []
print(all(list4))

Save and run the above code you should see the following output.

Output :

python all function code example 1 output

Explanation :

  • In the first case, It returns True because all the elements of the list were True
  • In the second case, It returns False because there was one False in the list.
  • In the third case, It returns False because all the elements in the list were False.
  • In the fourth case, It returns True because the list was empty.

Please note that all the above cases match exactly with the above-discussed definition.

Example 2

Let’s see how the all() behaves with other data types.

list1 = [1,1,0]
print(all(list1))

list2 = ['h','o',';']
print(all(list2))

list3 = [[],'k','0']

print(all(list3))

list4 = ['','k','0']
print(all(list4))

Output :

False
True
False
False

all() function behaves exactly with tuple and set as well.

But in the case of a dictionary, it checks if the key is True or not. here is the example for that.

Example 3

dict1 = {0:1,2:3}
print(all(dict1))

dict2 = {True:1,"k":0}
print(all(dict2))

dict3 = {'':5}
print(all(dict3))

Output :

False
True
False

It’s clear from the above example that it only checks keys and completely ignores the value of dict.

How Python Checks The Boolean Value Of Objects

Here is the algorithm that python uses while checking the boolean value of any object.

  • Python first checks if an object defines the __bool__ method.
  • If an object does not have __bool__ method then it checks for the __len__ method.
  • If an object does not have both __bool__ and __len__ then it returns True.

Base on the above algorithm let’s see how all() function behaves with the User-defined objects.

Example 4

class User:
    def __init__(self,name):
        self.name = name

    def __bool__(self):
        print("Bool is called")
        return True


l = [User("Bob"),User("ron")]
print(all(l))

It will return True after calling the __bool__ special method.

Output :

Bool is called
Bool is called
True
class User:
    def __init__(self,name):
        self.name = name

    def __len__(self):
        print("len is called")
        return len(self.name) != 0


l = [User("")]
print(all(l))

It does not have __bool__ method so python will call __len__ method.

Output :

len is called
False
class User:
    def __init__(self,name):
        self.name = name


l = [User("Bob"),User("ron")]
print(all(l))

It returns True because the User object does not define any of __bool__ and __len__ special methods.

Output :

True

That’s the wrap for the tutorial of python all function.

You can also learn about the any() builtin function Here.

Leave a Reply