Python

How to Use Any and All Functions in Python

This article will cover a guide on “any” and “all” built-in functions in Python. These functions can be used for checking the truthiness of each element in an iterable and then further logic can be written based on the return value.

All Function

The “all” function returns a “True” value if all elements in an iterable type are “True”. Take a look at the code sample below:

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

The “all” function is called by supplying it an iterable “item_list” as an argument. After running the above code sample, you should get the following output:

True

Here the “all” function has returned a “True” value as each and every element in the iterable type evaluates to be “True”.

Non-empty string type values are always considered to be “True”.

item_list = ["mango", "banana", "apple", "orange"]
print (all(item_list))

After running the above code sample, you should get the following output:

True

If the list is completely empty, “all” will always return “True”.

item_list = []
print (all(item_list))

After running the above code sample, you should get the following output:

True

If your iterable type contains empty string type elements, they are considered “False” when you call the “all” function on the iterable.

item_list = ["mango", "banana", "apple", ""]
print (all(item_list))

After running the above code sample, you should get the following output:

False

Since the last element (an empty string) in the “item_list” has been evaluated as “False”, the “all” function returns a “False” value.

If an element has a value of “0” (int type), that is “zero” in digits without quotes, it will be considered as “False” by “all” function:

item_list = ["mango", "banana", "apple", 0]
print (all(item_list))

After running the above code sample, you should get the following output:

False

Finally, if any element has a value of “False” (boolean type), the “all” function will return “False”:

item_list = ["mango", "banana", "apple", False]
print (all(item_list))

After running the above code sample, you should get the following output:

False

So it can be now safely assumed that if your iterable is not empty or does not contain the following three values, the “all” function will always return “True”:

  • “” (empty string)
  • 0 (without quotes, int type)
  • False (without quotes, boolean type)

Sometimes you may want to convert each value of an iterable to “True” or “False” based on certain conditions and then call “all” function on it. In such a case, you can use a list comprehension style syntax to directly feed “True” and “False” values to the “all” function:

item_list = [1, 2, 3, 4, 5, 6]
print (all(item > 2 for item in item_list))

In the “print” statement, a list comprehension style syntax is used where a “for” loop is run on “item_list” iterable. Each element in “item_list” is checked if it is greater than 2 or not and a value of “True” and “False” (boolean type) is assigned to it accordingly. The “all” function then evaluates each element for its truthiness. After running the above code sample, you should get the following output:

False

You can verify the output of list comprehension by adding an extra “print” statement:

item_list = [1, 2, 3, 4, 5, 6]
print (all(item > 2 for item in item_list))
print ([item > 2 for item in item_list])

After running the above code sample, you should get the following output:

False
[False, False, True, True, True, True]

Since not all values have a “True” value, the “all” function returns “False”.

Any Function

The “any” function returns a “True” value if any one element in an entire iterable type evaluates to be “True”. Take a look at the code sample below:

item_list = [True, True, True, False]
print (any(item_list))

The “any” function is called by supplying it an iterable “item_list” as an argument. After running the above code sample, you should get the following output:

True

Here the “any” function has returned a “True” value as at least one element in the iterable type evaluates to be “True”.

Non-empty string type values are always considered to be “True”.

item_list = ["mango", False]
print (any(item_list))

After running the above code sample, you should get the following output:

True

If the list / iterable type is completely empty, “any” will always return “False”.

item_list = []
print (any(item_list))

After running the above code sample, you should get the following output:

False

If your iterable type contains empty string type elements, they are considered “False” when you call the “any” function on the iterable.

item_list = ["", ""]
print (any(item_list))

Since both empty strings evaluates to “False”, you should get the following output after running the above code sample:

False

If an element has a value of “0” (int type), that is “zero” in digits without quotes, it will be considered as “False” by “any” function:

item_list = [0, 0]
print (any(item_list))

After running the above code sample, you should get the following output:

False

The list comprehension style syntax used in the “all” function above also applies to “any” function. For the sake of completion, it is explained here again.

Sometimes you may want to convert each value of an iterable to “True” or “False” based on certain conditions and then call “any” function on it. In such a case, you can use a list comprehension style syntax to directly feed “True” and “False” values to the “any” function:

item_list = [1, 2, 3, 4, 5, 6]
print (any(item > 2 for item in item_list))

In the “print” statement, a list comprehension style syntax is used where a “for” loop is run on “item_list” iterable. Each element in “item_list” is checked if it is greater than 2 or not and a value of “True” and “False” (boolean type) is assigned to it accordingly. The “any” function then evaluates each element for its truthiness. After running the above code sample, you should get the following output:

True

You can verify the output of list comprehension by adding an extra “print” statement:

item_list = [1, 2, 3, 4, 5, 6]
print (any(item > 2 for item in item_list))
print ([item > 2 for item in item_list])

After running the above code sample, you should get the following output:

True
[False, False, True, True, True, True]

Since at least one element has a “True” value, the “any” function returns “True”.

Conclusion

These are some of the useful ways you can use “any” and “all” functions in Python. While writing custom code that does the same job as “any” and “all” functions is not a big task, Python includes a lot of such small helper functions that reduces overall code verbosity and helps you do more with minimal code.

About the author

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.