Python any() Function
Description
The any() function is used to check if all elements in the given iterable argument are False. If they are all False, it returns False. If at least one element is True, it returns True.
Elements are considered True unless they are 0, empty, or False.
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
Here is the syntax for the any() method:
any(iterable)
Parameters
iterable-- A tuple or list.
Return Value
Returns False if all elements are empty, 0, or False. Returns True if not all elements are empty, 0, or False.
Examples
The following examples demonstrate the use of the any() method:
>>> any(['a', 'b', 'c', 'd']) # List, all elements are non-empty or non-zero
True
>>> any(['a', 'b', '', 'd']) # List, contains an empty element
True
>>> any([0, '', False]) # List, all elements are 0, '', False
False
>>> any(('a', 'b', 'c', 'd')) # Tuple, all elements are non-empty or non-zero
True
>>> any(('a', 'b', '', 'd')) # Tuple, contains an empty element
True
>>> any((0, '', False)) # Tuple, all elements are 0, '', False
False
>>> any([]) # Empty list
False
>>> any(()) # Empty tuple
False
YouTip