PythonBuilt-in functions
any()
Checks whether at least one item in a collection is truthy.
- Returns
TrueorFalse.
Example
#Python
scores = [72, 45, 88]
print(any(score < 50 for score in scores))
print(any(score > 90 for score in scores))
print(any([]))Resultsaved result, you can check it
True
False
False
Definition and usage
#The any() function returns True if any item is truthy, and False if all of them are falsy or the collection is empty. It stops checking at the first truthy item.
It is most often combined with a generator expression: any(score < 50 for score in scores) answers the question "did anyone fail?".
Together with all(), it lets you write conditions over entire collections in one readable line.
Syntax
#Syntax
any(iterable)Parameters
#iterable
a collection
The items to check.
Good practices
#- Pass a generator expression without square brackets, so
anycan stop checking early. - For an empty collection,
anyreturnsFalse.
Related entries
#- all()Checks whether every item in a collection is truthy.
- bool()Converts a value to True or False according to the rules of truthiness.
- filter()Returns an iterator of items for which a function returns a truthy value.
- List comprehensionsA concise way to build lists, sets, and dictionaries from other collections.
Spotted a mistake or missing an example? Write to us.