In python, we normally use the comparison operators and the logical operators to check for conditions for a different number of elements. What if you have to check for a condition in a list of elements? In this article, we will discuss the all()
function in python. We will also see how we can use the all()
function with different iterable objects.
What is all()
Function in Python?
The all()
function is used to check if all elements in an iterable object evaluate to True
or not. The all()
function takes an iterable object like a list, tuple, set, dictionary, or string as its input argument. After execution, it returns True
if all the elements of the iterable evaluate to True
. Otherwise, it returns False
. You can observe this in the following example.
myList1 = [1, 2, 3, 4]
myList2 = [1, True, False]
myList3 = []
print("The list is:", myList1)
output = all(myList1)
print("All the elements of the list evaluate to True:", output)
print("The list is:", myList2)
output = all(myList2)
print("All the elements of the list evaluate to True:", output)
print("The list is:", myList3)
output = all(myList3)
print("All the elements of the list evaluate to True:", output)
Output:
The list is: [1, 2, 3, 4]
All the elements of the list evaluate to True: True
The list is: [1, True, False]
All the elements of the list evaluate to True: False
The list is: []
All the elements of the list evaluate to True: True
You can understand the working of the all()
function as an application of the and
operator. For an iterable object having elements element1, element2, element3,.... elementN
, using the all()
function is equivalent to executing the statement element1 AND element2 AND element3 AND ….., AND elementN
.
The all() function with iterable objects
When we pass a list as an input argument to the all() function, it returns True if all the elements of the list evaluate to True.
myList1 = [1, 2, 3, 4]
myList2 = [1, True, False]
myList3 = []
print("The list is:", myList1)
output = all(myList1)
print("All the elements of the list evaluate to True:", output)
print("The list is:", myList2)
output = all(myList2)
print("All the elements of the list evaluate to True:", output)
print("The list is:", myList3)
output = all(myList3)
print("All the elements of the list evaluate to True:", output)
Output:
The list is: [1, 2, 3, 4]
All the elements of the list evaluate to True: True
The list is: [1, True, False]
All the elements of the list evaluate to True: False
The list is: []
All the elements of the list evaluate to True: True
When we pass an empty list to the all()
function, it returns True
. However, if there is an element in the list that evaluates to False
, the all()
function returns False
.
When we pass any string as an input argument to the all()
function, it returns True
.
myStr1 = "PythonForBeginners"
myStr2 = ""
print("The string is:", myStr1)
output = all(myStr1)
print("The output is:", output)
print("The string is:", myStr2)
output = all(myStr2)
print("The output is:", output)
Output:
The string is: PythonForBeginners
The output is: True
The string is:
The output is: True
For an empty string, the all()
function returns True
.
Similar to lists, when we pass a set as an input argument to the all()
function, it returns True
if all the elements of the set evaluate to True
.
mySet1 = {1, 2, 3, 4}
mySet2 = {1, 2, True, False}
mySet3 = set()
print("The Set is:", mySet1)
output = all(mySet1)
print("All the elements of the set evaluate to True:", output)
print("The Set is:", mySet2)
output = all(mySet2)
print("All the elements of the set evaluate to True:", output)
print("The Set is:", mySet3)
output = all(mySet3)
print("All the elements of the set evaluate to True:", output)
Output:
The Set is: {1, 2, 3, 4}
All the elements of the set evaluate to True: True
The Set is: {False, 1, 2}
All the elements of the set evaluate to True: False
The Set is: set()
All the elements of the set evaluate to True: True
When we pass an empty set to the all()
function, it returns True
.
The all() Function With Dictionaries in Python
When we pass a dictionary to the all()
function as the input argument, it returns True
if all the keys of the python dictionary evaluate to True
. Otherwise, it returns False
.
myDict1 = {1: 1, 2: 2, 3: 3, True: 4}
myDict2 = {False: 1, 1: 2, True: False}
myDict3 = {}
print("The Dictionary is:", myDict1)
output = all(myDict1)
print("All the keys of the dictionary evaluate to True:", output)
print("The Dictionary is:", myDict2)
output = all(myDict2)
print("All the keys of the dictionary evaluate to True:", output)
print("The Dictionary is:", myDict3)
output = all(myDict3)
print("All the keys of the dictionary evaluate to True:", output)
Output:
The Dictionary is: {1: 4, 2: 2, 3: 3}
All the keys of the dictionary evaluate to True: True
The Dictionary is: {False: 1, 1: False}
All the keys of the dictionary evaluate to True: False
The Dictionary is: {}
All the keys of the dictionary evaluate to True: True
When we pass an empty dictionary to the all()
function, it returns True.
Conclusion
In this article, we have discussed the all() function in python. We also used the all()
function with different iterable objects and observed the outputs of the function. To learn more about python programming, you can read this article on list comprehension.
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.