Introduction of Python all() function
Python all() is another Python built-in function.
As per the Python all() documentation, all() returns True if all elements of the iterable are true (or if the iterable is empty).
Syntax of all() built-in function
all(iterable)
Explanation
- all() is function name
- iterable could be anything, it could be the list, dictionary, set, tuple, etc.
- all() function returns True or False, it means it returns a Boolean value.
Examples of Python all() function
Let’s understand all() function in Python by taking a few examples
Using all() function with Python list
Below is an example that describes how we can use all() function with Python list.
Python list all () Example
animals = ["Cow", "Elephant", "Lion"] print("Elements of animals are",animals) print("type of animals is",type(animals)) tmp= all(animals) print(tmp) print("type of tmp is",type(tmp))
Output
Elements of animals are ['Cow', 'Elephant', 'Lion'] type of animals is <class 'list'> True type of tmp is <class 'bool'>
Explanation
- animals is the list we created. we can check the type of animals by using type function.
- we used tmp variable to get the result from all() function.
- print the value from tmp which is TRUE, as all the elements are present in list
- At last, we can check the type of tmp variable, which is bool.
Python list all() example with one empty element
animals = ["", "Elephant", "Lion"] print("Elements of animals are",animals) print("type of animals is",type(animals)) tmp= all(animals) print(tmp) print("type of tmp is",type(tmp))
Output
Elements of animals are ['', 'Elephant', 'Lion'] type of animals is <class 'list'> False type of tmp is <class 'bool'>
Explanation
In the above example, the first element of animals list is empty (“”) that’s why the output of all() function is False.
Python list all() example with number elements
#Python all() function with list dice = [1, 2, 3] print("Elements of animals are",dice) print("type of animals is",type(dice)) tmp= all(dice) print(tmp) print("type of tmp is",type(tmp))
Output
Elements of animals are [1, 2, 3] type of animals is <class 'list'> True type of tmp is <class 'bool'>
Explanation
Here dice is a list of numbers instead of String. The output is True as all the elements are present and they are non-zero. if any element is 0, then it returns False. see below example.
Python list all() example with 0 element
#Python all() function with list dice = [1, 2,0, 3] print("Elements of animals are",dice) print("type of animals is",type(dice)) tmp= all(dice) print(tmp) print("type of tmp is",type(tmp))
Output
Elements of animals are [1, 2, 3] type of animals is <class 'list'> False type of tmp is <class 'bool'>
Explanation
Here the output is False as one number is 0, Its important point to understand, if all the numbers are non-zero then all() function will return True. If any number in the iterable is 0, then the output is False.
Using all() with Python tuple
Below is examples of all() function in Python with Python tuple.
Example of Python all() with Python tuple
#Python all() function with tuple animals = ("Cow", "Elephant", "Lion") print("Elements of animals is",animals) print("type of animals is",type(animals)) tmp= all(animals) print(tmp) print("type of tmp is",type(tmp))
Output
Elements of animals is ('Cow', 'Elephant', 'Lion') type of animals is <class 'tuple'> True type of tmp is <class 'bool'>
Explanation
In the above example, we take same animals iterable but now animals is tuple here as elements of animals are started with round brackets( python tuples started with round brackets()).
As we have checked the type of animals using type function. Rest examples and uses cases of all function with Python tuple are same as we discussed in Python list.
Python all() function with set
Let’s see how all() function works in Python with set.
Example of Python set with all() function
#Python all() function with set animals = {"Cow", "Elephant", "Lion"} print("Elements of animals is",animals) print("type of animals is",type(animals)) tmp= all(animals) print(tmp) print("type of tmp is",type(tmp))
Output
Elements of animals is {'Cow', 'Lion', 'Elephant'} type of animals is <class 'set'> True type of tmp is <class 'bool'>
Using all() function with Python dictionary
Python dictionary is key-value pairs. below example will show how we can use function all() built-in function with Python dictionary.
Example of Python dictionary with all() function
#Python all() function with dictionary alphabets = {1:"one", 2:"two", 3:"three"} print("Elements of animals is",alphabets) print("type of animals is",type(alphabets)) tmp= all(alphabets) print(tmp) print ("*****************") meanings = {0:False, 1:True} print("Elements of meanings is",meanings) tmp2= all(meanings) print(tmp2)
Output
Elements of animals is {1: 'one', 2: 'two', 3: 'three'} type of animals is <class 'dict'> True ***************** Elements of meanings is {0: False, 1: True} False
Explanation
- Here alphabets is python dictionary, all functions return True as all key-value pairs are present and non zero.
- In the same example, we have another meanings dictionary. In this dictionary, we have two key-value pairs, 0 and 1. 0 means False, and 1 means True. so all() output will be False because there is one 0 key present.
In Python dictionary, if any key is 0 then all() function will return False
Python all() function on Empty Iterable
If iterable (list, tuple, set, dictionary etc ) is empty then all() function will return True
Example of all() function with empty iterable
#Python all() function with empty iterable ilist = list() # = ilist = [] ituple=tuple() # ituple=() iset=set() idict=dict() # idict={} tmp1= all(ilist) tmp2= all(ituple) tmp3= all(iset) tmp4= all(idict) print(tmp1) print(tmp2) print(tmp3) print(tmp4)
Output
True True True True
Explanation
we have created an empty list, empty tuple, empty set, empty dictionary. all() function will return True for all empty iterable.