Introduction: Python all() Function
When working with Python, there are many situations where you need to check whether every value in a collection satisfies a condition. Whether you’re validating multiple inputs, checking the status of several items, or ensuring all conditions are met, manually checking each value can make the code longer and less readable.
Without a built-in function, you would need to check each item individually, making the code more complex and harder to maintain.
The Python all() Function makes these checks simple and efficient.
What it is: The all() function is a built-in Python function that returns True only if all items in an iterable evaluate to True. If any item evaluates to a falsy value, it returns False.
Take a look at a quick example to understand how it works.
You can also explore where the function is used through its real-world use cases in Python.
Now let’s understand its syntax, parameters, and return value before exploring practical examples.
Syntax, Parameters, Return Value and Examples: Python all() Function
The following section explains the syntax, parameters, return value, and a quick example of the Python all() Function.
Syntax
all(iterable)
Parameters
| Parameter | Description |
|---|---|
iterable |
An iterable whose items are evaluated for their truth value, such as a list, tuple, set, dictionary, string, or other iterable object. |
Return Value
| Return Value | Description |
|---|---|
| bool | Returns True if all items in the iterable evaluate to True; otherwise, it returns False. |
Quick Example
The following example checks whether all values in a list evaluate to True using the all() function.
numbers = [5, 10, 15, 20]
print(all(numbers))
# Output:
True
The all() function returns True because every value in the list evaluates to True.
How the Python all() function works
- The
all()function accepts an iterable as its argument. - It evaluates each item based on its Boolean value.
- If every item is truthy, the function returns
True. - If any item is falsy, it returns
False. - The function stops checking as soon as it finds the first falsy value.
- The original iterable is not modified.
Examples: Python all() Function
The following examples show how the Python all() Function works in different programming scenarios.
Example 1: Checking a List Containing Truthy Values
values = [10, 20, 30]
print(all(values))
# Output:
True
Explanation: Since all values in the list are non-zero and evaluate to True, the all() function returns True.
Example 2: Checking a List Containing a Falsy Value
values = [10, 20, 0, 30]
print(all(values))
# Output:
False
Explanation: The value 0 is considered falsy, so the all() function returns False.
Example 3: Checking an Empty List
values = []
print(all(values))
# Output:
True
Explanation: An empty iterable returns True because it does not contain any falsy values. This behavior is known as the logical concept of vacuous truth.
Example 4: Validating User Input
numbers = input("Enter numbers separated by spaces: ").split()
numbers = [int(num) for num in numbers]
print(all(numbers))
# Sample Output:
Enter numbers separated by spaces: 10 20 30
True
Explanation: The entered numbers are converted into integers and the all() function checks whether every value evaluates to True.
Example 5: Checking Boolean Values
status = [True, True, True]
print(all(status))
# Output:
True
Explanation: Since every item in the iterable is True, the all() function returns True.
Example 6: Using all() with a Condition
numbers = [12, 25, 38, 41]
print(all(num > 10 for num in numbers))
# Output:
True
Explanation: The generator expression checks whether every number is greater than 10. Since all values satisfy the condition, the function returns True.
Use Cases: When to use the all() Function
Below are some common situations where the Python all() Function becomes useful:
- Checking whether all items in an iterable satisfy a condition.
- Validating multiple user inputs before processing data.
- Ensuring all required conditions are fulfilled in a program.
- Checking whether all files, services, or systems are available.
- Working with Boolean values in lists and other iterables.
- Reducing manual loops used only for validation checks.
- Writing cleaner and more readable conditional statements.
Key Takeaways: all() Function
The following key points summarize the most important concepts about the Python all() function.
- The
all()function returnsTrueonly when all items in an iterable evaluate toTrue. - It returns
Falseif any item is falsy. - An empty iterable returns
True. - It works with iterable objects such as lists, tuples, sets, dictionaries, and strings.
- The function stops evaluating as soon as it finds the first falsy value.
- The original iterable is not modified.
- It provides a simple and efficient alternative to manually checking every item in a loop.
all() is used with dictionaries, it evaluates dictionary keys by default. To check dictionary values, use the values() method.
In short, the Python all() Function provides a clean and efficient way to verify whether all values in an iterable meet a condition, making Python programs shorter, more readable, and easier to maintain.