any()

Checks whether at least one item in a collection is truthy.

Returns
True or False.
On this page

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 any can stop checking early.
  • For an empty collection, any returns False.

Related entries

#

Spotted a mistake or missing an example? Write to us.