Python all() Function

Python all() built-in function Tutorial. Learn to find out whether or not all the elements of any Python Iterable Object are True or not using this method.

Python all() Function

The Python built-in function all() is used to find out if all the elements inside a given Python Iterable Object are True or Not. This method only returns True if all the elements are True. If it encounters, even a single element that is not True, it will return the boolean value False.

A lot of beginners will have the question in mind in what cases, we’ll be needing to check if an iterable contains all the items as True. Well, this Python built-in function is quite useful in a lot of practical applications and you’ll find out the answer to this question after going through the Examples.

Syntax

booleanResult = all(iterable)

all() Parameters

The all() function takes any Python Iterable object such as Python List, Dictionary, Tuple, etc. as an argument.

Note. The definition of this function says that it checks if all the items are True. Most of you might be thinking the iterable elements needed only to be boolean items. But that’s not true. The elements can be even strings and numbers. If you’re wondering how this function will behave if the iterable will contain other data types than just boolean values, make sure to check all of the given examples.

all() Return Value

This method returns a boolean value i.e. either True or False.

  • If all the elements of the iterable object will be True or representing True, it will return True.
  • If at least one of the elements of the iterable object will be False or will indicate a False value, it will return False.
  • If the iterable object is empty, it also returns True.

Examples

The following python programs illustrate the working of the all() function.

Example 1. Checking all() Method For Different Iterables

#Boolean List with all items as True
list1 = [True, True, True]
#Numerical Elements
#0 Represents False and all other integers represents True
list2 = [1, 2, 3, 5]

#Boolean List with all items as False
list3 = [False, False, False]
#Numerical Elements
list4 = [0, 0, 0, 0]

#Atleast One-Element is False in the List
list5 = [True, False, True]
list6 = [1, 2, 0, 5]

#Some other Random cases
list7 = [1, 1, True, False, 1]
list8 = [9, False, 5, False]
list9 = [10, 20, 30, True]
list10 = [True, True, 100, 80, 0]

#all() Method Outputs
print(all(list1))
print(all(list2))
print(all(list3))
print(all(list4))
print(all(list5))
print(all(list6))
print(all(list7))
print(all(list8))
print(all(list9))
print(all(list10))

Observe the output carefully to understand the resultant boolean value for different use cases. Also read out the comments that are written in between the above python code snippet.

Python All() Function Example For Different List Elements

The following table gives the explanation for the output of each of the above 10 lists.

List Number Return Value Description
1 True The list contains all boolean values as True.
2 True The list does not contain the integer 0. All integers other than 0 represent True.
3 False All the boolean elements of the list are False.
4 False All the elements of the list are 0. Therefore, all elements represent the boolean value False.
5 False One of the elements is boolean False.
6 False The List contains the integer value 0 and it corresponds to the boolean value False.
7 False The false boolean value is counted as the list element.
8 False It contains two boolean False values.
9 True It contains all integers other than 0 that represent True and it also contains a direct boolean value True.
10 False It contains 0 representing, a False element.

If you have read the description for all of the above 10 outputs, you should be very clear about the expected output of the all() method for similar other use cases.

Example 2. Passing Strings as the Argument to the all() Function

In Python, Strings are also iterables and hence they can be passed to the method all() and this has been illustrated in the following example.

#All String Characters represents True
#Although 0 as an integer represents False
#But '0' as a String is True
string1 = "Hello, this is a Python Tutorial!"
string2 = "00000"
string3 = "The number starts with 0 and ends with 1."
string4 = "Why the number starts with 0?"

#Checking all() Method's Return Values
#All True
print(all(string1))
print(all(string2))
print(all(string3))
print(all(string4))

As you can observe in the output that all the resultant values of any of the specified string variables are True for the all() Method. This concludes that even if the 0 as an integer value represents False but if it is written as a String, it represents True.

Python All() Built In Method String Arguments Example

Example 3. Working of the all() Function when provided Dictionary as an Argument

Unlike most of the other iterables in which we only have a collection of items, a dictionary holds key and value pair items. In other words, each item in a dictionary contains a key and its value separately. Now when a dictionary is passed an argument to the all() method, it checks the boolean representation of the key and not the value.

Go through the following code carefully.

#Integer Keys and only the First Key is False
check1 = {0 : "True", 1 : False}

#String Keys, all True
check2 = {"0" : "False", "1": False}
check3 = {"a" : False, "b": True}

#Boolean Keys, second False
check4 = {True: "ok", False: "Not ok"}

print(all(check1))
print(all(check2))
print(all(check3))
print(all(check4))
  • In the first case, the first key is an integer key and it is also 0, therefore the returned value is False.
  • For the second case and the third case, the keys are strings. In the second case, even the key is 0 but as it is a String, the returned value of the all() function is True.
  • In the fourth case, the keys themselves are the boolean value and as the second key is the boolean value False itself, therefore, the output is False.

Python All() Method Example For Dictionaries

Example 4. all() Returns True for all empty Iterables

As mentioned earlier as well, for any iterable containing no elements, this method all() will always return True.

#Empty Dictionary
print(all({}))

#Empty list
print(all([]))

#Empty tuple
print(all(()))

#Empty String
print(all(""))

Python Built In Function All() Return Value For Empty Iterables Example

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

Leave a Reply

Your email address will not be published. Required fields are marked *