Python any() Function

Determines whether any item in an iterable is True

Usage

The any() function returns True if any item in an iterable is True. Otherwise, it returns False.

If the iterable is empty, the function returns False.

Syntax

any(iterable)

Python any() function parameters
ParameterConditionDescription
iterableRequiredAn iterable of type (list, string, tuple, set, dictionary etc.)

Falsy Values

In Python, all the following values are considered False.

  • Constants defined to be false: None and False.
  • Zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • Empty sequences and collections: '', (), [], {}, set(), range(0)

Basic Examples

# Check if any item in a list is True

L = [0, 0, 0]
print(any(L))   # Prints False

L = [0, 1, 0]
print(any(L))   # Prints True

Here are some scenarios where any() returns True.

L = [False, 0, 1]
print(any(L))   # Prints True

T = ('', [], 'green')
print(any(T))   # Prints True

S = {0j, 3+4j, 0.0}
print(any(S))   # Prints True

any() on a Dictionary

When you use any() function on a dictionary, it checks if any of the keys is true, not the values.

D1 = {0: 'Zero', 0: 'Nil'}
print(any(D1))   # Prints False

D2 = {'Zero': 0, 'Nil': 0}
print(any(D2))   # Prints True

any() on Empty Iterable

If the iterable is empty, the function returns False.

L = []
print(any(L))   # Prints False