Category: python
Understanding any()
Published on 27 Oct 2026
Explanation
any() returns True when at least one item is truthy and False for an empty iterable. It short-circuits after a truthy item.
Code:
scores = [30, 45, 80, 25] print(any(score >= 75 for score in scores))
Explanation
all() returns True when every item is truthy and also returns True for an empty iterable. It short-circuits after a falsy item.
Code:
scores = [80, 90, 75] print(all(score >= 35 for score in scores))
Explanation
Use any() when at least one item must match a condition, such as finding a negative number.
Code:
numbers = [10, 20, -5, 40] print(any(n < 0 for n in numbers))
Explanation
Use all() when every item must satisfy a condition, such as checking required fields.
Code:
user = {'name': 'Arun', 'email': 'arun@example.com'}
required = ['name', 'email']
print(all(user.get(key) for key in required))
Explanation
Combine them to express rules such as every mark being in range and at least one mark meeting a threshold.
Code:
marks = [45, 70, 82] valid = all(0 <= m <= 100 for m in marks) passed = any(m >= 50 for m in marks) print(valid, passed)