Python all() Function

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

all() Parameters

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

Note. As 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

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 NumberReturn ValueDescription
1TrueThe list contains all boolean values as True.
2TrueThe list does not contain the integer 0. All integers other than 0 represents True.
3FalseAll the boolean elements of the list are False.
4FalseAll the elements of the list are 0. Therefore, all elements represent the boolean value False.
5FalseOne of the elements is boolean False.
6FalseThe List contains the integer value 0 and it corresponds to the boolean value False.
7FalseThe false boolean value is counted as the list element.
8FalseIt contains Two boolean False values.
9TrueIt contains all integers other than 0 that represents True and it also contains a direct boolean value True.
10FalseIt 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.

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 of 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.

  • 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() the function is True.
  • In the fourth case, the keys itself 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.

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.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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