Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Any & All in Python?
Python provides two built-in functions any() and all() for evaluating boolean conditions across iterables. The any() function implements OR logic, while all() implements AND logic.
Python any() Function
The any() function returns True if at least one item in an iterable is true, otherwise it returns False. If the iterable is empty, it returns False.
Syntax
any(iterable)
The iterable can be a list, tuple, set, or dictionary.
Example with List
items = [False, True, False]
result = any(items)
print(result)
print("Result is True because at least one item is True")
True Result is True because at least one item is True
Example with Tuple
numbers = (0, 1, 0, False)
result = any(numbers)
print(result)
print("1 is truthy, so result is True")
True 1 is truthy, so result is True
Example with Set
values = {0, 1, 0} # Note: sets remove duplicates
result = any(values)
print(result)
print("Set contains:", values)
True
Set contains: {0, 1}
Example with Dictionary
# any() checks dictionary keys, not values
grades = {0: "Fail", 1: "Pass"}
result = any(grades)
print(result)
print("Key 1 is truthy, so result is True")
True Key 1 is truthy, so result is True
Python all() Function
The all() function returns True if all items in an iterable are true, otherwise it returns False. If the iterable is empty, it returns True.
Syntax
all(iterable)
Example with List
items = [True, True, False]
result = all(items)
print(result)
print("Result is False because one item is False")
False Result is False because one item is False
Example with Tuple
numbers = (0, True, False)
result = all(numbers)
print(result)
print("0 is falsy, so result is False")
False 0 is falsy, so result is False
Example with Set
values = {True, 1, 1} # True and 1 are equivalent
result = all(values)
print(result)
print("All values are truthy")
True All values are truthy
Example with Dictionary
grades = {0: "Fail", 1: "Pass"}
result = all(grades)
print(result)
print("Key 0 is falsy, so result is False")
False Key 0 is falsy, so result is False
Comparison
| Condition | any() Returns | all() Returns |
|---|---|---|
| All values are True | True | True |
| At least one value is True | True | False |
| All values are False | False | False |
| Empty iterable | False | True |
Practical Example
# Check if any student passed (score >= 60)
scores = [45, 78, 52, 91]
passed = any(score >= 60 for score in scores)
print(f"Any student passed: {passed}")
# Check if all students passed
all_passed = all(score >= 60 for score in scores)
print(f"All students passed: {all_passed}")
Any student passed: True All students passed: False
Conclusion
Use any() when you need OR logic (at least one condition must be true) and all() for AND logic (every condition must be true). Both functions work with generator expressions for efficient condition checking.
