The any() built-in function of python

The built-in function any() in Python returns True when at least one of the elements of an iterable object evaluates to true. The function returns False otherwise.

Built-in Functions Updated 17 Jan 2017 1 min read

Function signature

any(iterable)

Parameters

iterable - A Python iterator like list, deque, set and dictionary or any object implementing the Python iterator protocol.

Return value

Returns a Boolean value.

True when any of the elements from the iterabale is True. Returns False otherwise.

Overview

  • The Python built-in function any() checks whether a Python iterable object has at least one of its elements evaluating to True.
  • For example, a Python list object with only one element evaluating to True and all other elements as None will return True. If the iterable is empty the any() function will return False.
  • This behaviour is the opposite of all() function where all() will return True only if all the elements of an iterable evaluate to True.

Example

python
# Example Python program that finds whether any of the

            # elements of an iterable evaluates to True.

# Check a list which is sparsely populated with True values

            iterSparse = [7, None, None, None, None, None]

            print(any(iterSparse))

# Try an empty list

            emptyList = []

            print(any(emptyList))

# List without any values evaluating to True

            listOfNone = [None, None, None, None, None, None]

            print(any(listOfNone))

Output

Output
True

False

False