Python built-in function all() is used to judge whether all elements in an iterable object are Truthy and is an important tool for logical judgment.
1. Basic syntax
all(iterable)
Parameters: iterable must be an iterable object (such as lists, tuples, sets, dictionary values, etc.).
Return value:
- True : All elements are true values, or the iterable object is empty.
- False: There is at least one false value element.
2. Truth value judgment rules
The following values in Python are considered false values (Falsy), and the others are true values:
- Numbers: 0 , 0.0 , -0.0 , 0j , NaN , False .
- Empty containers: '' (empty string), (empty list), () (empty tuple), {} (empty dictionary/set).
- Special value: None.
Example:
print(all([1, 2, 3])) # True (all nonzero numbers are true)print(all([True, 0, "hello"])) # False (0 is false)print(all("")) # True (empty string is treated as true)print(all({1: False, 2: 5})) # True(Check the dictionary's keys,key1For false)
3. Typical usage scenarios
(1) Conditional batch verification
Check whether all of the conditions are met:
# Check whether all user inputs are non-emptyuser_inputs = ["yes", "2024", "male"] is_valid = all(() != "" for input in user_inputs) print(is_valid) # Output: True
(2) Data integrity check
Verify that all elements in the dataset meet the requirements:
# Check whether all values in the list are positivedata = [5, 10, 3, 0] # 0 is a false valueprint(all(x > 0 for x in data)) # Output: False
(3) Short circuit evaluation optimization
Return False immediately when the first false value is encountered to avoid unnecessary traversal:
large_list = [True] * 1000000 + [False] + [True] * 1000000 print(all(large_list)) # Quick returnFalse,No need to traverse subsequent elements
4. Comparison with other functions
function | Return value logic | Empty iterable object results |
all() | Return True when all elements are true | True |
any() | Return True when at least one element is true | False |
Example:
print(all([])) # True print(any([])) # False
5. Things to note
Non-iterable objects report an error:
If a non-iterable object (such as an integer) is passed in, a TypeError will be triggered:
all(123) # TypeError: 'int' object is not iterable
Dictionary processing:
all() checks its keys instead of values for dictionaries:
print(all({0: "a", 1: "b"})) # False(key0For false)
Efficiency of generator expressions
Combined with generator expressions, it can handle large data sets to avoid excessive memory usage:
# Check whether all lines in the file contain specific keywordswith open("") as f: all_contain_keyword = all("ERROR" in line for line in f)
6. Practical application examples
(1) Permission verification
permissions = ["read", "write", "execute"] user_permissions = ["read", "write"] print(all(perm in user_permissions for perm in permissions)) # False
(2) Configuration check
config = {"debug": False, "logging": True, "test_mode": False} print(all(())) # False(existFalsevalue)
Summarize
all() is a concise and efficient logical judgment tool in Python, suitable for batch verification conditions, data integrity check and other scenarios. Its short-circuit evaluation feature optimizes performance, but it should be noted that the parameters must be iterable objects, and the empty container's feature returning True may cause logical errors. Combining generators and conditional expressions can further improve the flexibility and efficiency of the code.
This is the end of this article about the implementation of the built-in Python function all(). For more related Python all() content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!