Python any() Function
any(iterable) returns True if at least one item of the iterable is truthy, and False if none are or the iterable is empty.any() answers the question "does at least one of these pass?" Is any order overdue? Does the message contain any banned word? Is any score below the pass mark? It replaces a loop with a flag variable by one readable line.
It is usually combined with a generator expression, such as any(s < 40 for s in scores). Its partner is all(), which asks the stricter question. The lesson on any and all walks through more patterns.
| Syntax | any(iterable) |
|---|---|
| Parameters | iterable: a list, tuple, generator or any other iterable |
| Returns | True or False |
| Empty iterable | any([]) is False |
| Time complexity | O(n) worst case; stops at the first truthy item |
How any() checks truthiness
any() looks at each item's truthiness: 0, '', None, False and empty containers count as false, nearly everything else as true. It stops reading as soon as it meets a truthy item. This is called short-circuiting, and it means the remaining items are never examined.
print(any([0, "", None]))
print(any([0, "", "ok"]))
print(any([]))Using any() with a generator expression
Usually you do not want to test the raw items, you want to test a condition on them. Write that condition as a generator expression inside the call. Because a generator produces values one at a time, any() can stop early without building a full list. Avoid putting square brackets inside, as any([... for ...]) creates the entire list first and loses the early exit.
def check(n):
print("checking", n)
return n > 1
print(any(check(n) for n in [1, 2, 3]))Only 1 and 2 are checked; 3 is never reached.
any() vs all() vs a for loop
| Question | Use | Empty input gives |
|---|---|---|
| Is at least one item true? | any(...) | False |
| Are all items true? | all(...) | True |
| Is no item true? | not any(...) | True |
| Is at least one item false? | not all(...) | False |
| Which item matched? | A for loop with break, or next(...) | Depends on your code |
Examples you can run
Press Run, then edit the code and run it again.
Is any score below the pass mark?
A generator expression tests each score.
scores = [72, 35, 88]
print(any(s < 40 for s in scores))Expected outputTrue
Check a message for banned words
in is evaluated for each banned word until one matches.
banned = ["spam", "scam"]
message = "this is not spam"
print(any(word in message for word in banned))Expected outputTrue
Short-circuiting stops early
The function prints each check so you can see where it stops.
def check(n):
print("checking", n)
return n > 1
print(any(check(n) for n in [1, 2, 3]))Expected outputchecking 1
checking 2
True
Is any field empty?
any() works on a list of booleans too.
form = {"name": "Asha", "email": "", "city": "Pune"}
print(any(value == "" for value in form.values()))Expected outputTrue
Common mistakes
Passing a single boolean
✗ Wrong
print(any(5 > 3))✓ Right
print(any(n > 3 for n in [1, 5]))any() needs an iterable. Use a generator expression to test each item.
Passing two arguments
✗ Wrong
print(any(1, 2))✓ Right
print(any([1, 2]))Put the values inside one list or tuple.
Comparing the result of any()
✗ Wrong
print(any([0, 2]) > 1)✓ Right
print(any(n > 1 for n in [0, 2]))Put the comparison inside the generator, not after any().
Trusting a non-empty string
✗ Wrong
print(any(["False", ""]))✓ Right
print(any(v == "True" for v in ["False", ""]))Compare explicitly, for example value == "True".
Python any() Function: questions people ask
What does any() return in Python?
It returns True if at least one item in the iterable is truthy, otherwise False. An empty iterable gives False.
What does any() do with an empty list?
any([]) returns False because there is no item that could be truthy.
Is any() the same as using or?
Yes in effect: any([a, b, c]) behaves like a or b or c but returns only True or False, and works on iterables of any length.
Keep learning
Step-by-step lessons:
Python any() and all()Python Truthy and Falsy ValuesPython List Comprehensions
Related:
Python all() FunctionPython filter() FunctionPython sum() FunctionPython isinstance() Function
More in built-in functions:
Python print() FunctionPython len() FunctionPython range() FunctionPython type() FunctionPython int() FunctionPython float() FunctionPython str() FunctionPython input() Function See all →