English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The all() method will return True when all elements of the given iterable are true. If not, it will return False.
The syntax of the all() method is:
all(iterable)
The all() method takes one parameter:
iterable - Any iterable containing elements (Lists,Tuples,Dictionariesetc.)
The all() method returns:
True-If all elements of iterable are true
False-If any element of iterable is false
Condition | Return value |
---|---|
All values are true | True |
All values are false | False |
A value is true (others are false) | False |
A value is false (others are true) | False |
Empty iterable | True |
# All values are true l = [1, 3, 4, 5] print(all(l)) # All values are false l = [0, False] print(all(l)) # A false value l = [1, 3, 4, 0] print(all(l)) # A value is true l = [0, False, 5] print(all(l)) # Empty iterable l = [] print(all(l))
When running the program, the output is:
True False False False True
The any() method is used in a similar way for tuples and similar listsSets.
s = "This is good" print(all(s)) # 0 is False # '0' is True s = '000' print(all(s)) s = '' print(all(s))
When running the program, the output is:
True True True
For dictionaries, if all keys (non-values) are true or the dictionary is empty, all() returns True. Otherwise, for all other cases, it returns false.
s = {0: 'False', 1: 'False'} print(all(s)) s = {1: 'True', 2: 'True'} print(all(s)) s = {1: 'True', False: 0} print(all(s)) s = {} print(all(s)) # 0 is False # '0' is True s = {'0': 'True'} print(all(s))
When running the program, the output is:
False True False True True