any() and all(): Quick Logical Checks

Published:
Last updated:
By Jeferson Peter
Python

Using any()

nums = [0, 0, 3, 0]
print(any(nums))  

# True (because 3 is truthy)
values = [x > 10 for x in [5, 20, 7]]
print(values)
print(any(values))

# [False, True, False]
# True (at least one condition is True)

Using all()

nums = [1, 2, 3, 4]
print(all(nums))

# True (all numbers are truthy)
values = [x > 0 for x in [5, 20, -3]]
print(values)
print(all(values))

# [True, True, False]
# False (not all conditions are True)

Combined example

passwords = ["abc123", "hello", "admin"]
print(any(p.isdigit() for p in passwords))  
print(all(len(p) >= 3 for p in passwords))

# True  (at least one has digits)
# True  (all have length >= 3)

Conclusion

Use any() when at least one condition should hold, and all() when every condition must be true. They make logical checks concise and clear.