Python any() Function

Short answer
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.

Quick facts
Syntaxany(iterable)
Parametersiterable: a list, tuple, generator or any other iterable
ReturnsTrue or False
Empty iterableany([]) is False
Time complexityO(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.

Python
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.

Python
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

QuestionUseEmpty 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.

Is any score below the pass mark?
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.

Check a message for banned words
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.

Short-circuiting stops early
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.

Is any field empty?
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))
TypeError: 'bool' object is not iterable

✓ 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))
TypeError: any() takes exactly one argument (2 given)

✓ Right

print(any([1, 2]))

Put the values inside one list or tuple.

Comparing the result of any()

✗ Wrong

print(any([0, 2]) > 1)
Prints `False`: `any()` returns `True`, and `True > 1` is false.

✓ 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", ""]))
Prints `True`, because the text "False" is a non-empty string and therefore truthy.

✓ 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 →