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 Example

Example: Check if any item in a list is True

# all false
L = [0, 0, 0]
x = any(L)
print(x)    # False
# one true
L = [0, 1, 0]
x = any(L)
print(x)    # True

More Examples

Example: Check if any item in a list is True

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

Example: Check if any item in a tuple is True

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

Example: Check if any item in a set is True

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

Example: Check if any item in a dictionary is True

D = {0: 'Zero', 0: 'Nil'}
x = any(D)
print(x)    # False

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

D = {'Zero': 0, 'Nil': 0}
x = any(D)
print(x)    # True

any() on Empty Iterable

If the iterable is empty, the function returns False.

Example:

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