Lesson explanation
After this lesson, you will be able to:
any() to detect whether at least one condition is true.all() to require every condition to be true.any([]) == False and all([]) == True when empty input is possible.When a decision depends on many values, avoid writing counters or long chains of or and and unnecessarily.
any(values) is true if at least one input is truthy.all(values) is true if every input is truthy.You can first turn data into booleans with a list comprehension. This lesson deliberately uses comprehensions you already know; generator expressions come later.
checks = [result == "ok" for result in results]For an empty input, any([]) is False, but all([]) is True. This is useful mathematically, but a release workflow usually requires at least one check before reporting that all checks passed. Add an explicit non-empty requirement when the business rule calls for it.
alerts = [False, True, False]
print(any(alerts))Output:
TrueOne truthy element is enough.
latencies = [42, 90, 110]
within_limit = [value <= 120 for value in latencies]
print(all(within_limit))Output:
TrueEvery comparison is true.
print(any([]))
print(all([]))Output:
False
TrueIf empty means “no checks were run,” you may need bool(checks) and all(checks) instead.
responses = [" PASS ", "pass", " FAIL "]
normalized = [value.strip().lower() for value in responses]
print(any([value == "fail" for value in normalized]))Output:
TrueNormalize before comparing so capitalization and spaces do not change the decision.
| Requirement | Check |
|---|---|
| One or more values satisfy a predicate | any([...]) |
| Every value satisfies a predicate | all([...]) |
| No values satisfy a predicate | not any([...]) |
| Require at least one value and all valid | bool(values) and all([...]) |
all([]) is True—it does not mean a real validation took place.any() means “at least one,” not “exactly one.”not any(...) with not all(...)—they describe different situations.