Any. Some elements in a list evaluate to False. These are things like 0, an empty sub-list, None, an empty string. With any() we see if any element is not false.
Notes, any. In our thinking, we can understand "any" to read "has any element that evaluates to True." The name "any" is easier to type than that phrase.
First example. Here we use the function with a list. When the lists has all zero elements, any returns false. But when we add a 1, any returns true.
So Any() requires at least one element that does not evaluate to False. Zero evaluates to False.
# Test any with all zero elements.# ... These evaluate to false.
values = [0, 0, 0]
result = any(values)
print("ANY", values, result)
# Test any with a 1 element.# ... This element evaluates to true, so any is true.
values = [0, 0, 1]
result = any(values)
print("ANY", values, result)ANY [0, 0, 0] False
ANY [0, 0, 1] True
False element examples. In Python, many things can evaluate to False—not just the value False. Here we find that None, an empty list, an empty string, and zero are all false.
Detail The any function will return False on this list. No element in the list is True.
# These elements all evaluate to false.# ... None, empty arrays, empty strings, and 0 are false.# ... So any is false.
values = [None, [], "", 0]
result = any(values)
print(result)False
Any versus all. Any and all are similar methods. But with all(), all elements must evaluate to True for the method to return true.
values = [0, None, "cat"]
# Test any() and all() functions on this list.# ... One "true" element means any is true.# ... But all() is false because there are 2 false elements.
print("ANY", values, any(values))
print("ALL", values, all(values))ANY [0, None, 'cat'] True
ALL [0, None, 'cat'] False
Empty list. The any function will return False for an empty list. Essentially any() searches for a True value. And an empty list will not have one.
# This is an empty list.# ... Any returns false.
empty = []
result = any(empty)
print(result)False
Benchmark, any. Testing the performance of Python is not easy. The implementation is important. Here we test any() and a for-loop over a list with an if-statement.
Version 1 This version of the code uses the any() method on a list. It ensures any() does not return False.
Version 2 Here we use a nested for-loop to implement the same logic that any() uses. The source code is somewhat longer.
Result In PyPy the for-loop is faster than any. Perhaps the compiler can optimize a nested loop faster than an any call.
import time
values = [0, None, 100]
print(time.time())
# Version 1: use any() function.
i = 0
while i < 10000000:
v = any(values)
if v == False:
break
i += 1
print(time.time())
# Version 2: implement any logic with for-loop.
i = 0
while i < 10000000:
t = False
for v in values:
if v:
t = True
break
if t == False:
break
i += 1
print(time.time())1453910914.887
1453910915.356 Version 1, any() = 0.46899986267 s
1453910915.528 Version 2, for, if = 0.17200016975 s
A summary. Any and all are useful methods in Python. Usually we prefer a for-loop. But in some situations these built-in functions can simplify code.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Nov 16, 2021 (image).