all() in Python | all() Function in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video courses

Overview

The all() in Python can be defined as the inbuilt function that returns TRUE if all the values in the given iterable object are TRUE. If not, it returns FALSE.

This article covers in detail the inbuilt function all() in python.

We will learn more about the all() function by going through examples with different iterable objects like Tuple, List, Set etc.

Syntax of all() in Python

The syntax of the all() in python is as follows:

Parameters of all() in Python

The all() function in python accepts a single parameter:

iterable: The iterable is considered an object such as a list, set, dictionary, tuple, etc.

Return Values of all() in Python

The return values of all() in python are: TRUE or FALSE.

True - The output is returned TRUE if all the elements in an iterable are TRUE. Even if the iterable object is empty while executing the all() function we get the output as TRUE.

False - The output is returned FALSE if any elements in an iterable are false.

For a better glance at all the possible scenarios, let us dive deeper below:

Possible ScenariosOutput Values
All iterable are trueTRUE
All iterable are falseFALSE
One iterable is true (others are false)FALSE
One iterable is false (others are true)FALSE
Empty IterableTRUE

Exceptions of all() in Python

As far as exceptions for all() in python are considered, we do need to keep in mind the following points:

  1. Whenever we are dealing with all() function with Dictionaries, we need to remember that the keys are evaluated for the scenarios mentioned above. It means that if all the keys in the dictionary are true, then the output is true. Otherwise, it returns false.
  2. Whenever we deal with integers like 0, 1, 2... in strings as we keep them in "" (inverted commas), the output is always TRUE.

Example

Output:

What is all() function in Python?

In this article, we will be covering the inbuilt function in python - The all() function.

  1. In python all() function, the iterable object ( list, tuples, set etc.) are taken as an argument.
  2. The all() in python can be considered an inbuilt function in Python that returns the output TRUE if all the values of a given iterable object like the Tuples, Strings, Set, List or Dictionary are TRUE.
  3. While executing, even if the iterable object is empty, the output turns out to be TRUE.
  4. This also implies that the python all() function returns FALSE if any element in an iterable object like Tuples, Strings, List or Dictionary is FALSE.

More Examples

Example 1: all() in Python with Lists

Output:

Example 2: all() in Python with Tuples

Output:

Example 3: all() in Python with Sets

Output:

Example 4: all() in Python with Dictionaries

PRO TIP: Whenever we are dealing with all() function with Dictionaries, we need to remember that the keys are evaluated for the scenarios mentioned above. It means that if all the keys of the dictionary are true, then the output is true , or else it returns false.

Output:

Example 5: all() in Python with Strings

Whenever we are dealing with all() function with the iterable object as strings, we can have the following two scenarios majorly:

  1. NON- EMPTY Strings which give output as TRUE.
  2. EMPTY Strings which give output as TRUE.

PRO TIP: Whenever we deal with integers like 0, 1, 2..in strings as we keep them in "" (inverted commas), the output is always TRUE.

Let us dive into the example below to understand the concept explained above.

Output:

Example 6: all() function with Condition

Output:

:::

Conclusion

  1. The all() in python returns the output as TRUE if all elements in the given iterable object are true.
  2. The empty iterable in all() function in python is also considered as TRUE; hence, the output we get is TRUE.
  3. The python all() function returns the output as FALSE if any elements in the given iterable object are FALSE.
  4. The syntax for all() in Python is all(iterable), where the single parameter is an iterable object like a list, set, dictionary, tuples etc.
  5. The iterable objects such as lists, tuples, dictionaries, and sets align with all the five scenarios possible for the all() in python.
  6. The string object follows two major scenarios with respect to the all() function in python, that is, a. NON- EMPTY Strings, which gives output as TRUE. b. EMPTY Strings, which gives output as TRUE.
  7. The all() function in python also works well with conditions in an iterable.

See Also

  1. filter() in python
  2. any() in python