Python all() function returns true if all values of the passed iterable object are true else it returns false.
Basically, all() Function in python checks whether all elements in the list, tuple, dictionary, etc is true or not.
Syntax:
1 | all(iterable_object) |
Python all() Parameters
iterable_object – Any iterable which can contain more than one elements e.g list, dictionary, set, tuple, etc
all() in Python returns only true or false depending on the elements.
Python all() Examples
Example 1: List
1 2 | myList = [-1, 8, 4, 2] print(all(myList)) |
1 | True |
1 2 | myList = [-1, 8, 4, 2, 0] print(all(myList)) |
1 | False |
Any integer except 0 is considered as true in Python.
Example 2: Dictionary
1 2 | mydict = {4: "A", 2: "B", 3: "C"} print(all(mydict)) |
1 | True |
1 2 | mydict = {4: "A", 2: "B", 3: "C", 0: "D"} print(all(mydict)) |
1 | False |
all() check keys instead of values for the dictionary.
Example 3: String
1 2 3 4 5 6 7 8 | myStr = "Pencil Programmer" print(all(myStr)) myStr = "0 to 9" print(all(myStr)) myStr = "0" print(all(myStr)) |
1 2 3 | True True True |
Though 0
in Python means false but '0'
in Python is a character which has non-zero ASCII code therefore result into True.
Comment below your suggestion or doubts.