Python 3 reference · Built-in Functions

Python any() Function

Return True if at least one item in an iterable is truthy. Returns False for an empty iterable and stops at the first truthy item.

Python 3 Since 3.0

Syntax

any(iterable)

Parameters

ParameterDescription
iterable Any iterable.

Return value

True if any item is truthy, otherwise False.

Examples

Basic useTry it yourself
print(any([0, "", None, 5]))
print(any([0, "", None]))
print(any([]))

Output

True
False
False
Is there at least one match?Try it yourself
words = ["apple", "banana", "kiwi"]
print(any(len(w) > 5 for w in words))
print(any(w.startswith("z") for w in words))

Output

True
False
Look for any banned wordTry it yourself
banned = {"spam", "scam"}
message = "this is not a scam, promise"
print(any(word in banned for word in message.replace(",", "").split()))

Output

True

Good to know

  • any() is the opposite check of all(): "at least one" instead of "every".
  • It short-circuits: with a generator it stops reading as soon as it finds a truthy item.

Version history

VersionChange
Python 3.0Added