The any() and all() functions are built-in Python functions used to check conditions across multiple values.
They are especially useful when working with:
- Lists
- Tuples
- Sets
- Generator expressions
- Boolean values
- Data validation
- Student marks
- User permissions
- Form validation
1. any() Function
The any() function returns:
Trueif at least one item is truthy.FalseIf all items are falsy.
Syntax
any(iterable)Example:
numbers = [False, False, True, False]
result = any(numbers)
print(result)Output:
TrueBecause at least one value is True.
2. Basic any() Example
values = [False, False, False, True]
print(any(values))Output:
True3. All Values Are False
values = [False, False, False]
print(any(values))Output:
FalseThere is no truthy value.
4. One True Value
values = [False, False, True, False]
print(any(values))Output:
TrueOnly one True is enough for any() to return True.
5. any() with Numbers
Python considers:
0 → False
Non-zero number → TrueExample:
numbers = [0, 0, 5, 0]
print(any(numbers))Output:
TrueBecause 5 is truthy.
6. All Numbers Are Zero
numbers = [0, 0, 0, 0]
print(any(numbers))Output:
False7. any() with Conditions
This is one of the most useful patterns.
numbers = [10, 20, 30, 45, 50]
result = any(
x > 40
for x in numbers
)
print(result)Output:
TrueWhy?
Because 45 and 50 are greater than 40.
8. Check Whether Any Number Is Even
numbers = [1, 3, 5, 7, 8]
result = any(
x % 2 == 0
for x in numbers
)
print(result)Output:
TrueThere is an even number: 8.
9. Check Whether Any Number Is Negative
numbers = [10, 20, -5, 30]
result = any(
x < 0
for x in numbers
)
print(result)Output:
True10. Check Whether Any Student Passed
marks = [25, 30, 35, 45]
result = any(
mark >= 40
for mark in marks
)
print(result)Output:
TrueAt least one student has passed.
11. Check Whether Any Student Failed
marks = [75, 85, 35, 90]
result = any(
mark < 40
for mark in marks
)
print(result)Output:
TrueAt least one student has failed.
12. any() with Strings
names = ["Ram", "", "Sita"]
print(any(names))Output:
TrueBecause non-empty strings are truthy.
13. Empty Strings
names = ["", "", ""]
print(any(names))Output:
FalseAll strings are empty.
14. any() with None
values = [None, None, "Python"]
print(any(values))Output:
TrueBecause "Python" is truthy.
15. Practical Example : User Login
Suppose we have several login attempts:
login_attempts = [False, False, True]
if any(login_attempts):
print("User logged in successfully")
else:
print("Login failed")Output:
User logged in successfully16. all() Function
The all() function returns:
Trueif every item is truthy.Falseif at least one item is falsy.
Syntax
all(iterable)Example:
values = [True, True, True]
print(all(values))Output:
True17. Basic all() Example
values = [True, True, True, True]
print(all(values))Output:
TrueEvery value is True.
18. One False Value
values = [True, True, False, True]
print(all(values))Output:
FalseOnly one False is enough for all() to return False.
19. all() with Numbers
numbers = [1, 2, 3, 4, 5]
print(all(numbers))Output:
TrueAll numbers are non-zero.
20. Zero Makes all() False
numbers = [1, 2, 0, 4]
print(all(numbers))Output:
FalseBecause 0 is falsy.
21. Check Whether All Numbers Are Positive
numbers = [10, 20, 30, 40]
result = all(
x > 0
for x in numbers
)
print(result)Output:
TrueEvery number is greater than zero.
22. One Negative Number
numbers = [10, 20, -5, 40]
result = all(
x > 0
for x in numbers
)
print(result)Output:
FalseBecause -5 is not greater than zero.
23. Check Whether All Numbers Are Even
numbers = [2, 4, 6, 8]
result = all(
x % 2 == 0
for x in numbers
)
print(result)Output:
True24. One Odd Number
numbers = [2, 4, 7, 8]
result = all(
x % 2 == 0
for x in numbers
)
print(result)Output:
FalseBecause 7 is odd.
25. Check Whether All Students Passe
marks = [75, 85, 65, 90]
result = all(
mark >= 40
for mark in marks
)
print(result)Output:
TrueEvery student has passed.
26. One Student Failed
marks = [75, 85, 35, 90]
result = all(
mark >= 40
for mark in marks
)
print(result)Output:
FalseBecause one student has marks below 40.
27. any() vs all()
This is the most important difference.
| Function | Meaning |
|---|---|
any() | At least one must be true |
all() | Every value must be true |
Example:
numbers = [2, 4, 6, 8]any(x > 5 for x in numbers)Result:
TrueBecause 6 and 8 are greater than 5.
But:
all(x > 5 for x in numbers)Result:
FalseBecause 2 and 4 are not greater than 5.
28. any() with List Comprehension
You can technically use a list comprehension:
numbers = [1, 2, 3, 4, 5]
result = any([
x > 3
for x in numbers
])
print(result)Output:
TrueBut this creates an unnecessary list.
A generator expression is better:
result = any(
x > 3
for x in numbers
)This avoids creating the intermediate list.
29. all() with Generator Expression
Similarly:
numbers = [2, 4, 6, 8]
result = all(
x % 2 == 0
for x in numbers
)
print(result)This is memory-efficient and commonly used.
30. Short-Circuit Behavior
any() and all() can stop processing as soon as the answer is known.
For any():
False
False
True
↓
STOPOnce True is found, there is no need to check the remaining values.
For all():
True
True
False
↓
STOPOnce False is found, the result is already known.
This is called short-circuit evaluation.
31. Practical Example : Password Validation
Suppose a password must contain at least one number.
password = "Python123"
has_number = any(
character.isdigit()
for character in password
)
print(has_number)Output:
True32. Check for Uppercase Character
password = "python123A"
has_uppercase = any(
character.isupper()
for character in password
)
print(has_uppercase)Output:
True33. Check for Special Character
password = "Python@123"
special_characters = "@#$%&!"
has_special = any(
character in special_characters
for character in password
)
print(has_special)Output:
True34. Validate Password Completely
We can combine all() and any().
password = "Python@123"
has_uppercase = any(
char.isupper()
for char in password
)
has_lowercase = any(
char.islower()
for char in password
)
has_number = any(
char.isdigit()
for char in password
)
has_special = any(
char in "@#$%&!"
for char in password
)
long_enough = len(password) >= 8
if all([
has_uppercase,
has_lowercase,
has_number,
has_special,
long_enough
]):
print("Valid password")
else:
print("Invalid password")Output:
Valid passwordThis is a practical combination of any() and all().
35. Practical Example : Product Validation
Suppose product prices must all be positive.
prices = [80000, 1500, 3000, 25000]
valid = all(
price > 0
for price in prices
)
print(validOutput:
True36. Check Whether Any Product Is Expensive
prices = [1500, 3000, 25000, 5000]
expensive = any(
price > 20000
for price in prices
)
print(expensive)Output:
True37. Practical Example : Course Eligibility
Suppose a student must score at least 40 in every subject.
marks = [65, 75, 55, 80]
eligible = all(
mark >= 40
for mark in marks
)
print(eligible)Output:
True38. Check Whether Any Subject Has Failed
marks = [65, 75, 35, 80]
has_failed = any(
mark < 40
for mark in marks
)
print(has_failed)Output:
True39. Empty Iterable
There is an important special case.
print(any([]))Output:
FalseBut:
print(all([]))Output:
TrueThis can seem strange at first.
The reason is based on the logical definitions of "any" and "all":
- No element can make
any()true →False. - No element violates the requirement of
all()→True.
For beginner-level programming, remember the behavior rather than worrying about the formal logic.
40. any() and all() with Dictionaries
By default, iterating over a dictionary checks its keys.
data = {
1: "Python",
2: "Django",
3: "MERN"
}
print(any(data))Output:
TrueBecause the keys 1, 2, and 3 are truthy.
If you want to check values:
data = {
"Ram": 75,
"Sita": 85,
"Hari": 65
}
result = all(
mark >= 40
for mark in data.values()
)
print(result)Output:
True41. Practical Data Validation
Suppose we have employee records:
employees = [
{"name": "Ram", "age": 25},
{"name": "Sita", "age": 28},
{"name": "Hari", "age": 30}
]Check whether everyone is an adult:
result = all(
employee["age"] >= 18
for employee in employees
)
print(result)Output:
TrueCheck whether anyone is older than 29:
result = any(
employee["age"] > 29
for employee in employees
)
print(result)Output:
True42. any() and all() with Previous Topics
You have now covered several useful Python tools.
filter()
Select values:
filter(
lambda x: x > 40,
marks
)map()
Transform values:
map(
lambda x: x * 2,
numbers
)Generator expression
Generate values lazily:
(x * 2 for x in numbers)any()
Check whether at least one satisfies a condition:
any(
x > 40
for x in numbers
)all()
Check whether every value satisfies a condition:
all(
x > 0
for x in numbers
)Quick Comparison
filter()
↓
Select values
map()
↓
Transform values
Generator expression
↓
Generate values lazily
any()
↓
Is AT LEAST ONE true?
all()
↓
Are ALL true?Important Points to Remember
any()
any(iterable)Returns True when at least one item is truthy.
Example:
any([False, False, True])Result:
Trueall()
all(iterable)Returns True when every item is truthy.
Example:
all([True, True, True]Result:
TrueWith conditions
any(
x > 50
for x in numbers
)all(
x > 0
for x in numbers
)Easy way to remember
ANY → At least ONE
ALL → Every ONE