Python any() Function

Python 内置函数Python Built-in Functions


Description

The any() function is used to determine whether the given iterable parameter is all False. If all are False, it returns False; if any one is True, it returns True.

Elements other than 0, empty, and FALSE are considered TRUE.

The function is equivalent to:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

Available in Python 2.5 and above.

Syntax

The following is the syntax of the any() method:

any(iterable)

Parameters

  • iterable -- tuple or list.

Return Value

If all are empty, 0, or false, return false; if not all are empty, 0, or false, return true.


Examples

The following demonstrates examples of using the any() method:

>>>any(['a', 'b', 'c', 'd']) # list, elements are all not empty or 0 True >>> any(['a', 'b', '', 'd']) # list, there is one empty element True >>> any([0, '', False]) # list, elements are all 0, '', false False >>> any(('a', 'b', 'c', 'd')) # tuple, elements are all not empty or 0 True >>> any(('a', 'b', '', 'd')) # tuple, there is one empty element True >>> any((0, '', False)) # tuple, elements are all 0, '', false False >>> any([]) # empty list False >>> any(()) # empty tuple False

Python 内置函数Python Built-in Functions

Other Extensions