The all()
function in Python returns True if all elements in the iterable are true. If the iterable is empty, it will return True. It checks the truth value of all elements in the iterable using their inherent boolean value.
Parameter Values
Parameter | Description |
---|---|
iterable | An iterable object (such as a |
Return Values
The all()
function in Python returns a bool
value: True
or False
.
How to Use all()
in Python
Example 1:
The all()
function returns True if all items in an iterable are true, otherwise it returns False.
data = [True, True, True]
result = all(data)
print(result) # Output: True
Example 2:
It can be used with a list comprehension to check conditions for all elements in a list.
numbers = [1, 2, 3, 4, 5]
result = all(x > 0 for x in numbers)
print(result) # Output: True
Example 3:
If any item in the iterable is false, the all()
function will return False.
data = [True, False, True]
result = all(data)
print(result) # Output: False