Python all() Function
all()It is a built-in function in Python used to determine whether all elements in an iterable object are true.
all()It iterates over all elements in the iterable, and if the boolean value of all elements isTrue, then it returnsTrue; otherwise it returnsFalse. If the iterable is empty, it also returnsTrue。
Word Definition: allIt means "all", indicating "all elements are true".
Basic Syntax and Parameters
Syntax Format
all(iterable)
Parameter Description
- Parameter iterable:
- Type: Iterable object (list, tuple, set, dictionary, string, etc.)
- Description: The iterable object to check.
Function Description
- Return Value: Returns a Boolean value
TrueorFalse。 - Empty Iterable: An empty iterable returns
True(This is a mathematical convention).
Examples
Example 1: Basic Usage
Example
# List all True
print(all([True, True, True])) # Output: True
# List contains False
print(all([True, False, True])) # Output: False
# List all non-zero numbers
print(all([1, 2, 3])) # Output: True
print(all([1, 0, 3])) # Output: False
# Empty list
print(all([])) # Output: True
print(all([True, True, True])) # Output: True
# List contains False
print(all([True, False, True])) # Output: False
# List all non-zero numbers
print(all([1, 2, 3])) # Output: True
print(all([1, 0, 3])) # Output: False
# Empty list
print(all([])) # Output: True
Expected output:
True False True False True
Code analysis:
- Returns True when all elements are true (non-zero, non-empty, non-None).
- Returns False as long as one element is false.
- Empty list returns True (this is Python's convention).
Example 2: Other Iterable Objects
Example
# Tuple
print(all((1, 2, 3))) # Output: True
print(all((1, 0, 3))) # Output: False
# Set
print(all({1, 2, 3})) # Output: True
print(all({1, 0, 3})) # Output: False
# Dictionary (checks keys)
print(all({"a": 1, "b": 2})) # Output: True
print(all({"": 1})) # Output: False (empty key is false)
# String
print(all("hello")) # Output: True
print(all("")) # Output: True (empty string)
# Generator
gen = (x > 0 for x in [1, 2, 3])
print(all(gen)) # Output: True
print(all((1, 2, 3))) # Output: True
print(all((1, 0, 3))) # Output: False
# Set
print(all({1, 2, 3})) # Output: True
print(all({1, 0, 3})) # Output: False
# Dictionary (checks keys)
print(all({"a": 1, "b": 2})) # Output: True
print(all({"": 1})) # Output: False (empty key is false)
# String
print(all("hello")) # Output: True
print(all("")) # Output: True (empty string)
# Generator
gen = (x > 0 for x in [1, 2, 3])
print(all(gen)) # Output: True
Expected output:
True False True False True False True True True
Code analysis:
- Tuples and sets are used in the same way as lists.
- The dictionary checks keys, not values.
- The ASCII code of every character in a string is not 0, so a non-empty string returns True.
Example 3: Practical Applications
Example
# Validate user input
def validate_user(user):
required_fields = ['username', 'email', 'password']
return all(user.get(field) for field in required_fields)
user1 = {'username': 'tom', 'email': 'tom@example.com', 'password': '123'}
user2 = {'username': 'tom', 'email': '', 'password': '123'}
print(validate_user(user1)) # Output: True
print(validate_user(user2)) # Output: False
# Check all numbers are positive
numbers = [1, 2, 3, 4, 5]
print(all(x > 0 for x in numbers)) # Output: True
numbers = [1, -2, 3]
print(all(x > 0 for x in numbers)) # Output: False
# Check if list contains specific elements
colors = ['red', 'green', 'blue']
required = ['red', 'blue']
print(all(c in colors for c in required)) # Output: True
def validate_user(user):
required_fields = ['username', 'email', 'password']
return all(user.get(field) for field in required_fields)
user1 = {'username': 'tom', 'email': 'tom@example.com', 'password': '123'}
user2 = {'username': 'tom', 'email': '', 'password': '123'}
print(validate_user(user1)) # Output: True
print(validate_user(user2)) # Output: False
# Check all numbers are positive
numbers = [1, 2, 3, 4, 5]
print(all(x > 0 for x in numbers)) # Output: True
numbers = [1, -2, 3]
print(all(x > 0 for x in numbers)) # Output: False
# Check if list contains specific elements
colors = ['red', 'green', 'blue']
required = ['red', 'blue']
print(all(c in colors for c in required)) # Output: True
Expected output:
True False True False True
all()Often used in scenarios such as data validation and condition checking, it can make code more concise and Pythonic.
Other Extensions
Python3 Built-in Functions