Python all() Function
Description
The all() function is used to determine whether all elements in the given iterable parameter are TRUE. If so, it returns True; otherwise, it returns False.
Elements other than 0, empty, None, and False are considered True.
The function is equivalent to:
def all(iterable):
for element in iterable:
if not element:
return False
return True
for element in iterable:
if not element:
return False
return True
Available in Python 2.5 and above.
Syntax
The following is the syntax for the all() method:
all(iterable)
Parameters
- iterable -- tuple or list.
Return Value
If all elements of iterable are not 0, '', False, or if iterable is empty, all(iterable) returns True; otherwise, it returns False.
Note:Empty tuples and empty lists return True. Special attention is needed here.
Examples
The following shows examples of using the all() method:
>>> all(['a', 'b', 'c', 'd']) # list, all elements are not empty or 0
True
>>> all(['a', 'b', '', 'd']) # list, there is an empty element
False
>>> all([0, 1,2, 3]) # list, there is an element that is 0
False
>>> all(('a', 'b', 'c', 'd')) # tuple, all elements are not empty or 0
True
>>> all(('a', 'b', '', 'd')) # tuple, there is an empty element
False
>>> all((0, 1, 2, 3)) # tuple, there is an element that is 0
False
>>> all([]) # empty list
True
>>> all(()) # empty tuple
True
True
>>> all(['a', 'b', '', 'd']) # list, there is an empty element
False
>>> all([0, 1,2, 3]) # list, there is an element that is 0
False
>>> all(('a', 'b', 'c', 'd')) # tuple, all elements are not empty or 0
True
>>> all(('a', 'b', '', 'd')) # tuple, there is an empty element
False
>>> all((0, 1, 2, 3)) # tuple, there is an element that is 0
False
>>> all([]) # empty list
True
>>> all(()) # empty tuple
True
Python Built-in Functions