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
| Parameter | Description |
|---|---|
iterable |
Any iterable. |
Return value
True if any item is truthy, otherwise False.
Examples
print(any([0, "", None, 5]))
print(any([0, "", None]))
print(any([]))
Output
True False False
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
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 ofall(): "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
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
all()· Return True if every item in an iterable is truthy, or if the iterable is empty. Stops at the first falsy item.filter()· Keep only the items of an iterable for which a function returns True. Returns a lazy iterator, not a list.bool()· Convert a value to True or False using Python's truth rules: zero, empty and None are False, everything else is True.- Python Booleans · tutorial