How to Use all() and any() in Python
In Python, you can use the built-in functions all()
and any()
to check whether all elements or at least one element in an iterable (such as a list or tuple) evaluate to True
.
- Built-in Functions - all() — Python 3.13.3 documentation
- Built-in Functions - any() — Python 3.13.3 documentation
Truth Value Testing in Python
Although Python has a bool
type with True
and False
, it also evaluates other types (like numbers and strings) as true or false in conditional statements.
The following objects are considered false:
- constants defined to be false:
None
andFalse
- zero of any numeric type:
0
,0.0
,0j
,Decimal(0)
,Fraction(0, 1)
- empty sequences and collections:
''
,()
,[]
,{}
,set()
,range(0)
Built-in Types - Truth Value Testing — Python 3.13.3 documentation
All other objects are considered true.
For more details, see the following article.
These rules also apply when determining truth values in all()
and any()
.
all()
Checks if All Elements Are True
all()
returns True
if all elements in the given iterable evaluate to true. It returns False
if any element is false.
print(all([True, True, True]))
# True
print(all([True, False, True]))
# False
You can use all()
with any iterable type, including lists, tuples, and sets.
print(all((True, True, True)))
# True
print(all({True, True, True}))
# True
As mentioned above, all()
evaluates objects of any type. For example, empty strings and 0
are considered false, while other strings and numbers are considered true.
print(all(['aaa', 'bbb', 'ccc']))
# True
print(all(['aaa', 'bbb', 'ccc', '']))
# False
print(all([1, 2, 3]))
# True
print(all([0, 1, 2, 3]))
# False
all()
is equivalent to the following code:
def all(iterable):
for element in iterable:
if not element:
return False
return True
Therefore, all()
returns True
for an empty iterable.
print(all([]))
# True
any()
Checks if Any Element Is True
any()
returns True
if at least one element in the given iterable evaluates to true. It returns False
if all elements are false.
print(any([True, False, False]))
# True
print(any([False, False, False]))
# False
You can use any()
with any iterable type, including lists, tuples, and sets.
print(any((True, False, False)))
# True
print(any({True, False, False}))
# True
Objects of types other than bool
are also evaluated, as mentioned above.
print(any(['aaa', 'bbb', 'ccc', '']))
# True
print(any(['', '', '', '']))
# False
print(any([0, 1, 2, 3]))
# True
print(any([0, 0, 0, 0]))
# False
any()
is equivalent to the following code:
def any(iterable):
for element in iterable:
if element:
return True
return False
Therefore, any()
returns False
for an empty iterable.
print(any([]))
# False
not any()
Checks if All Elements Are False
Because any()
returns True
when any element is true and False
when all elements are false, you can use not any()
to check if all elements are false.
print(not any([False, False, False]))
# True
print(not any([True, False, False]))
# False
all()
and any()
with Conditions
Apply all()
and any()
to List Comprehensions and Generator Expressions
While previous examples directly evaluated elements as true or false, you can use comprehensions to apply all()
or any()
with specific conditions.
For example, you can check if all elements meet a certain condition.
You can evaluate each element in an iterable against a condition using list comprehensions:
l = [0, 1, 2, 3, 4]
print([i > 2 for i in l])
# [False, False, False, True, True]
Passing this result to all()
or any()
lets you check if all or any elements meet the condition.
print(all([i > 2 for i in l]))
# False
print(any([i > 2 for i in l]))
# True
Changing []
to ()
creates a generator expression, which returns a generator instead of a list.
print(type([i > 2 for i in l]))
# <class 'list'>
print(type((i > 2 for i in l)))
# <class 'generator'>
When a generator expression is the only argument, you can omit the parentheses and pass it directly to functions like all()
or any()
.
print(all(i > 2 for i in l))
# False
print(any(i > 2 for i in l))
# True
Benefits of Using Generator Expressions
Unlike lists, generators process elements one at a time, which can improve performance by reducing processing time and memory usage.
For example, consider a list of 100,000 consecutive numbers:
l = list(range(100000))
print(l[:5])
# [0, 1, 2, 3, 4]
print(l[-5:])
# [99995, 99996, 99997, 99998, 99999]
print(len(l))
# 100000
Let's compare the processing time of list comprehensions and generator expressions with all()
and any()
. Note that the following examples use Jupyter Notebook's %%timeit
magic command and won't work in regular Python scripts.
For all()
, the result is determined to be False
as soon as a False
value is found.
A list comprehension evaluates the expression (in this case, i < 0
) for all elements, creates a list, and passes it to all()
or any()
. In contrast, a generator expression processes elements one at a time. Since all()
stops at the first False
value, a generator expression can be can be significantly faster when a False
value appears early in the iterable.
Note: Units in the %%timeit
output can differ (ms
for milliseconds, μs
for microseconds, and ns
for nanoseconds), so take care when comparing results.
%%timeit
all([i < 0 for i in l])
# 963 μs ± 22.8 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
%%timeit
all(i < 0 for i in l)
# 132 ns ± 4.21 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
If all elements are True
(the result of all()
is True
), a generator expression is not faster since it needs to process all elements until the end. In the example, it may even be slower.
%%timeit
all([i >= 0 for i in l])
# 1.11 ms ± 18.2 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
%%timeit
all(i >= 0 for i in l)
# 1.75 ms ± 2.53 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
Thus, the processing time for generator expressions varies depending on where the first False
is found. If the middle element is determined to be False
, processing time could be halved.
%%timeit
all(i < 50000 for i in l)
# 882 μs ± 2.92 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
The same applies to any()
. The result is determined to be True
if there is at least one True
. In the case of generator expressions, the processing ends as soon as True
is encountered.
%%timeit
any([i >= 0 for i in l])
# 902 μs ± 5.31 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
%%timeit
any(i >= 0 for i in l)
# 123 ns ± 0.0354 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
%%timeit
any([i < 0 for i in l])
# 1.1 ms ± 2.88 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
%%timeit
any(i < 0 for i in l)
# 1.75 ms ± 6.04 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
%%timeit
any(i > 50000 for i in l)
# 897 μs ± 6.9 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
Count Elements Meeting a Condition
Since True
is treated as 1
and False
as 0
, you can use sum()
to count how many elements meet a given condition.
print(sum(i > 2 for i in l))
# 2
To count the number of False
, use not
.
print(sum(not (i > 2) for i in l))
# 3