Python any() Function
any()is a built-in function in Python used to determine whether there is at least one truthy value in an iterable object.
any()It iterates through all elements in the iterable; if the boolean value of any element isTrue, then it returnsTrue; otherwise, it returnsFalse. If the iterable is empty, it returnsFalse。
Word Meaning: anyIt means "any", indicating "there exists at least one that is true".
Basic Syntax and Parameters
Syntax Format
any(iterable)
Parameter Description
- Parameter iterable:
- Type: Iterable object
- Description: The iterable object to check.
Function Description
- Return value: Returns a boolean value
TrueorFalse。 - Empty iterable: An empty iterable returns
False。
Examples
Example 1: Basic Usage
Example
# List contains True
print(any([False, True, False])) # Output: True
# List is all False
print(any([False, False, False])) # Output: False
# List contains non-zero numbers
print(any([0, 1, 0])) # Output: True
print(any([0, 0, 0])) # Output: False
# Empty list
print(any([])) # Output: False
print(any([False, True, False])) # Output: True
# List is all False
print(any([False, False, False])) # Output: False
# List contains non-zero numbers
print(any([0, 1, 0])) # Output: True
print(any([0, 0, 0])) # Output: False
# Empty list
print(any([])) # Output: False
Expected output:
True False True False False
Code explanation:
- As long as one element is truthy (non-zero, non-empty, non-None), it returns True.
- Only when all elements are falsy does it return False.
- An empty list returns False.
Example 2: Other Iterable Objects
Example
# Tuple
print(any((0, 0, 1))) # Output: True
print(any((0, 0, 0))) # Output: False
# Set
print(any({0, 1, 2})) # Output: True
print(any({0, 0, 0})) # Output: False
# Dictionary (checks keys)
print(any({"a": 0, "b": 1})) # Output: True
print(any({"": 0})) # Output: False (empty key is falsy)
# String
print(any("hello")) # Output: True (all characters have ASCII > 0)
print(any("")) # Output: False (empty string)
# Generator
gen = (x > 5 for x in [1, 2, 3])
print(any(gen)) # Output: False
print(any((0, 0, 1))) # Output: True
print(any((0, 0, 0))) # Output: False
# Set
print(any({0, 1, 2})) # Output: True
print(any({0, 0, 0})) # Output: False
# Dictionary (checks keys)
print(any({"a": 0, "b": 1})) # Output: True
print(any({"": 0})) # Output: False (empty key is falsy)
# String
print(any("hello")) # Output: True (all characters have ASCII > 0)
print(any("")) # Output: False (empty string)
# Generator
gen = (x > 5 for x in [1, 2, 3])
print(any(gen)) # Output: False
Expected output:
True False True False True False True False False
Code explanation:
- Tuples, sets, and dictionaries are used the same way as lists.
- The ASCII code of each character in a string is not 0, so a non-empty string returns True.
Example 3: Practical Application
Example
# Check if there is a negative number in the list
numbers = [1, 2, -3, 4, 5]
has_negative = any(x < 0 for x in numbers)
print(f"Has negative: {has_negative}") # Output: Has negative: True
# Check if the string contains specific characters
text = "Hello World"
vowels = "aeiou"
has_vowel = any(c in vowels for c in text.lower())
print(f"Contains vowels: {has_vowel}") # Output: Contains vowels: True
# Validate user input
def validate_input(data):
required_keys = ['username', 'password']
return any(data.get(key) for key in required_keys)
user1 = {'username': 'tom', 'password': ''}
user2 = {'username': '', 'password': '123'}
user3 = {'username': 'tom', 'password': '123'}
print(validate_input(user1)) # Output: True
print(validate_input(user2)) # Output: True
print(validate_input(user3)) # Output: True
numbers = [1, 2, -3, 4, 5]
has_negative = any(x < 0 for x in numbers)
print(f"Has negative: {has_negative}") # Output: Has negative: True
# Check if the string contains specific characters
text = "Hello World"
vowels = "aeiou"
has_vowel = any(c in vowels for c in text.lower())
print(f"Contains vowels: {has_vowel}") # Output: Contains vowels: True
# Validate user input
def validate_input(data):
required_keys = ['username', 'password']
return any(data.get(key) for key in required_keys)
user1 = {'username': 'tom', 'password': ''}
user2 = {'username': '', 'password': '123'}
user3 = {'username': 'tom', 'password': '123'}
print(validate_input(user1)) # Output: True
print(validate_input(user2)) # Output: True
print(validate_input(user3)) # Output: True
Expected output:
有负数: True 包含元音字母: True True True True
any()andall()are complementary functions, commonly used for data validation and condition checking.
Other Extensions
Python3 Built-in Functions