The python function all(), takes an iterable as parameter and returns True if all elements in the iterative are TRUE or it returns FALSE.
all(iterable) #Where iterable can be a list, string, tuple, dictionary , set etc
The all() takes one and only one mandatory parameter. Any iterable can be passed as a parameter to the all() method.
Parameter | Description | Required / Optional |
---|---|---|
Iterable |
Any iterable such as list, tuple, string, dictionary etc | Required |
Returns a boolean, either True or False. Returns True only if all the elements in the iterable is True and when the iterable is empty. In all other cases the function returns False.
Input | Output |
---|---|
All values in the iterable True | True |
All values in the iterable False | False |
All except one element in the iterable True | False |
All except one element in the iterable False | False |
Empty iterable | True |
l = [1, 3, 4, 5]
print(all(l))
# all values false l = [0, False] print(all(l))
t = (1,0,1)
print(all(t)) t= ()
print(all(t))
Output:
True False False True
s = "This is a non empty string" print(all(s))
s = '000'
print(all(s))
s = ''
print(all(s))
While passing a string as a parameter, the most important thing is that '0' is True and 0 is False
Output:
True True True
d = {1: 'False', 2: 'False'} print(all(d))
d = {0: 'True', 2: 'True'} print(all(d))
d = {1: 'True', False: 0} print(all(d))
d = {}
print(all(d))
For dictionaries, the function returns True if all the keys are true, regardless of the values.
Output:
True False False True