Skip to main content
The any() built-in function evaluates an iterable and returns True if at least one element within the iterable evaluates to a truthy value. If the iterable is empty or all elements evaluate to a falsy value, it returns False.

Parameters

  • iterable: Any Python object capable of returning its members one at a time, such as a list, tuple, set, dict, str, or generator.

Mechanics and Short-Circuit Evaluation

Under the hood, any() applies Python’s standard truth value testing (bool()) to each element. It utilizes short-circuit evaluation, meaning it halts iteration and returns True the exact moment it encounters the first truthy element. It does not evaluate the remainder of the iterable. The underlying logic is equivalent to the following Python model:

Truthy vs. Falsy Evaluation

To understand any(), you must understand Python’s falsy values. any() will only return False if the iterable is empty or consists entirely of the following falsy objects:
  • Constants: None and False
  • Numeric zeros: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • Empty sequences and collections: "", (), [], {}, set(), range(0)

Evaluation Behavior

Standard Iterables
Dictionaries When passed a dictionary, any() evaluates the dictionary’s keys, not its values, because standard dictionary iteration yields keys.
Generators Because of short-circuit evaluation, any() consumes a generator only up to the first truthy value. The generator’s state is preserved for subsequent calls.
Tired of Poor Python Skills? Fix That With Deep Grasping!Learn More