any()
Returns True when at least one item in a list or other iterable is true, and False when every item is false or the iterable is empty.
any(iterable)any() looks through a collection and answers one question: is at least one of these items true? As soon as it finds a true item it stops and returns True. If it reaches the end without finding one, it returns False.
It shines together with a generator expression, a short for written inside the brackets: any(price > 100 for price in prices) reads almost like English and replaces a loop with a flag variable.
"True" here means truthy: non-zero numbers, non-empty strings and non-empty lists count as true, while 0, "", [] and None count as false. An empty collection has no true item, so any([]) is False.
Parameters
| Parameter | What it is |
|---|---|
iterablerequired | Any collection you can loop over: a list, tuple, set, string, or a generator expression that produces True/False values. |
Return value
True if at least one item is truthy, otherwise False.
Examples
1Is any value true?
print(any([False, False, True]))
print(any([0, 0, 0]))
print(any([0, "", None, "hello"]))True False True
"hello" is a non-empty string, so it counts as true.
2Check a condition with a generator expression
Write the test once inside any(); Python runs it for each price until one passes.
prices = [12.50, 8.99, 149.00, 23.40]
if any(p > 100 for p in prices):
print("At least one item costs more than 100")
print(any(p < 5 for p in prices))At least one item costs more than 100 False
3Does a password contain a digit?
A string is a collection of characters, so any() can check them one by one.
for password in ["sunshine", "sunsh1ne"]:
has_digit = any(ch.isdigit() for ch in password)
print(password, "->", has_digit)sunshine -> False sunsh1ne -> True
4Find a banned word in a comment
banned = ["spam", "scam", "free money"]
comment = "Click here for FREE MONEY now"
text = comment.lower()
print(any(word in text for word in banned))True
5Empty collections
print(any([]))
orders = []
if not any(orders):
print("No orders yet")False No orders yet
With nothing to check, there is no true item, so the answer is False.
Common mistakes
Passing several values instead of one collection
any() takes exactly one argument: the collection. any(a, b) raises TypeError. Put the values in a list or tuple first.
a, b = 0, 5
try:
any(a, b)
except TypeError:
print("TypeError: any() takes one collection")
print(any([a, b]))TypeError: any() takes one collection True
Writing a list comprehension when a generator is enough
any([x > 0 for x in nums]) builds the whole list first and checks every item. any(x > 0 for x in nums) (round brackets only) stops at the first match, which is faster on big data. Both give the same answer.
Questions people ask
What is the difference between any() and all()?
any() needs at least one true item; all() needs every item to be true. On an empty list any() gives False and all() gives True.
How do I check if any item of a list is in another list?
Use any(x in other for x in items). For big lists, turn other into a set first so each lookup is fast: other = set(other).
How do I find which item matched, not just True?
any() only says yes or no. To get the item, use next((x for x in items if test(x)), None), which returns the first match or None. See next().
Does any() check every item?
No. It stops at the first true item. This is called short-circuiting, and it means the rest of a generator is never run.